Skip to content

Tracing a bug back through dbt's compiled SQL

When a dbt model fails, the error often comes from the compiled SQL, not the Jinja source, since Jinja/macros/ref() calls resolve before the SQL reaches the warehouse. Checking the compiled code in the target/compiled directory (or the 'Compiled code' tab in dbt Cloud) shows exactly what the warehouse executed, making it the fastest way to isolate syntax or logic errors. Running the compiled SQL directly in your warehouse's query editor lets you iterate quickly without waiting for dbt to re-run the full Jinja compilation each time.

1 · Learn the must-know

  • Compiled SQL lives in target/compiled/<project_name>/models/... and mirrors the exact file/folder path of the source model.
  • target/run/ contains the actual SQL that was executed against the warehouse (post-compilation, including any adapter-specific wrapping), which can differ slightly from target/compiled for incremental or materialization logic.
  • Use dbt compile (or dbt run --select model_name) to regenerate compiled files before inspecting them, since stale target/ artifacts can mislead debugging.
  • Copy-pasting compiled SQL into your warehouse's native SQL editor lets you test fixes, check row counts, and inspect intermediate CTEs faster than repeatedly invoking dbt.
  • Errors referencing ref() or source() failures usually indicate a graph/compilation-time problem, while errors on the raw SQL syntax usually indicate a compiled-code problem, so identifying which stage failed narrows debugging quickly.
  • The target/ directory is gitignored and regenerated per invocation, so always re-run dbt compile after changing a model or its Jinja logic before trusting the compiled output.

2 · Check your understanding

Check this objectiveFree · always available

A dbt run on models/marts/fct_orders.sql fails with: Database Error in model fct_orders column reference "customer_id" is ambiguous The model joins stg_orders to stg_customers in Jinja using {{ ref(...) }} calls, and no column is explicitly aliased. What should you inspect first to pinpoint the source of the ambiguity?

Your objective map0 tried · 0 answered correctly · 31 untouched

What you have tried across dbt Analytics Engineering's objectives, not a readiness score.

Developing and optimizing dbt models45.16% of the exam*0 of 14 tried
Managing dbt models governance9.68% of the exam*0 of 3 tried
Debugging data modeling errors16.13% of the exam*0 of 5 tried
Troubleshooting and optimizing dbt pipelines6.45% of the exam*0 of 2 tried
Implementing dbt tests9.68% of the exam*0 of 3 tried
Implementing and maintaining external dependencies6.45% of the exam*0 of 2 tried
Leveraging the dbt state6.45% of the exam*0 of 2 tried

* Our estimate. dbt Labs publishes no section weights.

3 · Keep going