Skip to content

dbt Model Compilation Error Fix

DodaTech Updated 2026-06-24 3 min read

In this tutorial, you'll learn about dbt model compilation error fix. We cover key concepts, practical examples, and best practices.

You run dbt run and get Compilation Error in model my_model (models/my_model.sql) - Database Error in model my_model — the model references a source or ref that does not exist, or there is a Jinja syntax error.

Step-by-Step Fix

1. Check the compilation error

dbt run --model my_model --debug 2>&1 | tail -50

Expected error messages:

Compilation Error in model my_model (models/my_model.sql)
  Model "ref('non_existent_model')" was not found

2. Fix missing ref() references

-- Wrong — referencing a model that does not exist
SELECT * FROM {{ ref('non_existent_model') }}

-- Right — use the correct model name from the project
SELECT * FROM {{ ref('stg_orders') }}

3. Fix source() references

-- Wrong — missing source.yml declaration
SELECT * FROM {{ source('raw', 'orders') }}

-- Right — define the source first in schema.yml
-- models/schema.yml:
version: 2
sources:
  - name: raw
    database: my_db
    schema: raw
    tables:
      - name: orders

-- Then reference it
SELECT * FROM {{ source('raw', 'orders') }}

4. Fix Jinja syntax errors

-- Wrong — unmatched Jinja tags
SELECT *
FROM {{ ref('orders' }}
WHERE status = 'active'

-- Right — balanced tags
SELECT *
FROM {{ ref('orders') }}
WHERE status = 'active'

5. Run dbt compile to debug

dbt compile --model my_model

Expected output:

Compilation completed successfully.
Target located at: target/compiled/my_project/models/my_model.sql

Common Mistakes

Mistake Fix
Model name typo in ref() Check model names with dbt list --resource-type model
Source not defined in YAML Add source configuration in schema.yml
Missing closing Jinja tag Always ensure {{ }} and {% %} are properly closed
Using wrong database/schema reference Set correct database and schema in source/destination config
Undefined variable in configuration Ensure all variables used in {{ var('my_var') }} are defined in dbt_project.yml

Prevention

  • Run dbt compile before dbt run to catch errors early.
  • Use dbt --warn-error to treat warnings as errors in CI/CD.
  • Implement dbt build which runs dbt run, dbt test, and dbt snapshot in one command.
  • Version control all YAML source definitions alongside models.

DodaTech Tools

Doda Browser's dbt lineage visualizer maps model dependencies and highlights compilation errors. DodaZIP archives compiled SQL and model backups for version tracking. Durga Antivirus Pro scans dbt models for SQL injection vulnerabilities in Jinja templates.

Common Mistakes with model error

  1. Forgetting deriving (Show, Eq) on custom data types needed for debugging
  2. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
  3. Using head and tail instead of pattern matching, causing runtime errors on empty lists

These mistakes appear frequently in real-world DBT code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

What is the difference between ref() and source() in dbt?

ref() references another dbt model within the same project. source() references raw database tables defined in source YAML files. ||| How do I fix "Model not found" for a model that exists? The model might be disabled in schema.yml or outside the model-paths configuration. Check dbt_project.yml for model-paths and the model's enabled flag. ||| Why does dbt compile fail with "Server error"? The database connection is failing. Check the profile in profiles.yml: verify host, port, database, and credentials. Run dbt debug to test the connection.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro