What if one small change could break your entire data pipeline--and how <code>ref()</code> stops that from happening?
Why ref() function for model dependencies in dbt? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have many data tables that depend on each other, and you write SQL queries manually to join or use them. Every time you change one table's name or location, you must update all queries by hand.
This manual way is slow and risky. You might miss some places where the table is used, causing errors or outdated results. It's hard to keep track of all connections, especially as projects grow.
The ref() function in dbt solves this by letting you refer to models by name. dbt automatically manages dependencies and updates references if models change, so your project stays organized and error-free.
SELECT * FROM raw_sales_data JOIN raw_customer_data ON ...
SELECT * FROM {{ ref('sales_data') }} JOIN {{ ref('customer_data') }} ON ...With ref(), you can build complex data pipelines confidently, knowing dependencies are tracked and updated automatically.
A data analyst updates a sales model name. Thanks to ref(), all dependent models update automatically, avoiding broken reports and saving hours of manual fixes.
Manual table references are error-prone and hard to maintain.
ref() manages model dependencies automatically.
This leads to safer, cleaner, and more scalable data projects.
Practice
ref() function in dbt?Solution
Step 1: Understand the role of
Theref()ref()function is used to link one model to another in dbt, so dbt knows the order to run models and their dependencies.Step 2: Identify what
It does not write raw SQL, create users, or schedule runs. Its main role is linking models.ref()does not doFinal Answer:
To link models and define dependencies between them -> Option CQuick Check:
ref() links models = A [OK]
- Thinking ref() writes SQL code
- Confusing ref() with scheduling tools
- Assuming ref() manages database users
customers inside another model using ref()?Solution
Step 1: Recall dbt Jinja syntax for ref()
In dbt,ref()must be wrapped in double curly braces and the model name must be a string in quotes.Step 2: Check each option
select * from {{ ref('customers') }} uses{{ ref('customers') }}which is correct. Options B and C miss the curly braces or quotes. select * from {{ ref(customers) }} misses quotes around the model name.Final Answer:
select * from {{ ref('customers') }} -> Option AQuick Check:
Use {{ ref('model_name') }} syntax = A [OK]
- Omitting curly braces {{ }}
- Not putting model name in quotes
- Using ref() without Jinja syntax
orders model exists?select order_id, customer_id
from {{ ref('orders') }}Solution
Step 1: Understand what ref() compiles to
Theref()function compiles to the actual table name of the referenced model, usually just the model name like 'orders'.Step 2: Check the compiled SQL output
The compiled SQL replaces{{ ref('orders') }}withorders, so the output isselect order_id, customer_id from orders.Final Answer:
select order_id, customer_id from orders -> Option AQuick Check:
ref('orders') compiles to orders = C [OK]
- Leaving ref() uncompiled in SQL
- Adding extra schema prefix without config
- Using ref() as a string literal
select * from ref('sales')When you run dbt, you get an error. What is the problem?
Solution
Step 1: Check the syntax of ref() usage
In dbt,ref()must be wrapped in double curly braces to be interpreted as Jinja code.Step 2: Identify the error cause
The code usesref('sales')without{{ }}, so dbt treats it as plain text, causing an error.Final Answer:
Missing double curly braces around ref() -> Option DQuick Check:
Use {{ ref('model') }} not ref('model') alone = D [OK]
- Forgetting {{ }} around ref()
- Assuming ref() works without Jinja
- Removing quotes from model name
customers and orders. You want to create a new model customer_orders that joins these two. Which is the best way to use ref() to ensure correct dependencies and flexible naming?Solution
Step 1: Use ref() with correct Jinja syntax for both models
To link models and ensure dbt knows dependencies, use{{ ref('model_name') }}for bothcustomersandorders.Step 2: Avoid hardcoding table names or missing Jinja syntax
Options A and C hardcode names or use quotes incorrectly. select c.customer_id, o.order_id from ref('customers') c join ref('orders') o on c.customer_id = o.customer_id misses curly braces, so it won't compile.Final Answer:
select c.customer_id, o.order_id from {{ ref('customers') }} c join {{ ref('orders') }} o on c.customer_id = o.customer_id -> Option BQuick Check:
Use {{ ref('model') }} for all dependencies = B [OK]
- Hardcoding table names instead of using ref()
- Forgetting curly braces around ref()
- Using quotes incorrectly around model names
