dbt: Debugging dbt Models with –debug


๐Ÿ› ๏ธ Debugging dbt Models with --debug

When working with dbt models, youโ€™re not writing raw SQL โ€” youโ€™re writing templated SQL using Jinja, which includes:

  • {{ ref('...') }}
  • {{ source('...') }}
  • Custom macros
  • Conditional logic with if/else

Before dbt runs your models, it must compile them into plain SQL that your data warehouse (e.g., Snowflake, BigQuery, Redshift, etc.) can understand.

โœ… The --debug flag helps you:

  1. See the actual SQL being run.
  2. Get precise error messages from the data warehouse.
  3. Understand failures related to logic, joins, schema references, etc.

๐Ÿ“ฆ Example

Letโ€™s say you have a model: models/orders_summary.sql

select
  customer_id,
  count(*) as order_count
from {{ ref('orders') }}
group by customer_id

Now you run the model:

dbt run --select orders_summary --debug

๐Ÿงพ Output (simplified example):

16:23:45  Running with dbt=1.7.0
16:23:46  Found 1 model, 0 tests, 0 snapshots

16:23:46  Concurrency: 1 threads (target='dev')
16:23:46  1 of 1 START model analytics.orders_summary ............. [RUN]
16:23:46  SQL:
  select
    customer_id,
    count(*) as order_count
  from analytics.raw_orders
  group by customer_id

16:23:47  Finished running 1 model in 1.23s.

16:23:47  Completed successfully

โœ… Notice how {{ ref('orders') }} is replaced with the actual table name (analytics.raw_orders).

If there was a problem (e.g. a typo, or the column doesnโ€™t exist), the debug output would include the error from the database:

Database Error: column "customer_id" does not exist at line 2

๐Ÿ” When is --debug especially useful?

ScenarioHow --debug helps
โŒ Model failsShows exact SQL that failed + warehouse error
โ“ Unexpected dataLets you check the logic that was actually compiled
๐Ÿ”„ Macros misbehavingReveals how macro output looks in final SQL
๐Ÿงช Testing JinjaHelps debug if conditions, loops, and dynamic refs
๐Ÿ“š CI/CD issuesUseful for debugging failed pipelines (especially in logs)

โœ… Bonus: See compiled SQL in files

You can also inspect compiled SQL in the target/compiled/ directory:

cat target/compiled/your_project/models/orders_summary.sql

This shows exactly what will be sent to your data warehouse โ€” without even running it.


๐Ÿงผ Summary

  • --debug prints the fully rendered SQL sent to your warehouse.
  • It helps you catch:
    • ๐Ÿงฑ Broken refs
    • ๐Ÿ’ฌ Jinja logic errors
    • ๐Ÿ“‰ Data warehouse SQL errors
  • Essential for faster debugging, especially in complex projects using macros and conditionals.

Leave a Reply

Your email address will not be published. Required fields are marked *