0
0
dbtdata~10 mins

ref() function for model dependencies in dbt - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - ref() function for model dependencies
Start: Model A
Call ref('Model B')
dbt finds Model B
Build dependency graph
Run Model B first
Run Model A after Model B
Complete execution
The ref() function tells dbt which models depend on others, so it builds and runs them in the right order.
Execution Sample
dbt
select * from {{ ref('customers') }}
where active = true
This code uses ref() to get data from the 'customers' model before filtering active ones.
Execution Table
StepActionInputOutputNotes
1Start building 'orders' modelorders model SQL with ref('customers')Detect dependency on 'customers'ref() identifies 'customers' as dependency
2Check if 'customers' model is builtDependency graphNot built yetMust build 'customers' first
3Build 'customers' modelcustomers model SQLTable/view createdCustomers data ready
4Build 'orders' modelorders model SQL with ref resolvedTable/view createdOrders built using customers data
5Execution complete--All models built in correct order
💡 All dependent models built; ref() resolved dependencies correctly
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
dependency_graphemptycontains 'customers'checks 'customers' builtadds 'customers' builtadds 'orders' builtcomplete
model_statusnone built'customers' needed'customers' building'customers' built'orders' buildingall built
Key Moments - 2 Insights
Why does dbt build the 'customers' model before 'orders'?
Because the 'orders' model uses ref('customers'), dbt knows 'orders' depends on 'customers' and builds 'customers' first (see execution_table step 2 and 3).
What happens if you forget to use ref() and just write the table name?
dbt won't know about the dependency, so it might build models in the wrong order, causing errors or stale data (reflected in dependency_graph variable_tracker).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does dbt start building the 'customers' model?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Check the 'Action' column in execution_table for when 'customers' model is built.
According to variable_tracker, what is the state of 'model_status' after Step 3?
A'orders' building
B'customers' built
Cnone built
D'customers' needed
💡 Hint
Look at the 'model_status' row and the column 'After Step 3' in variable_tracker.
If you remove ref('customers') from 'orders' model, what likely happens?
Adbt builds both models simultaneously without issues
Bdbt ignores 'orders' model
Cdbt builds 'orders' before 'customers' causing errors
Ddbt automatically detects dependency anyway
💡 Hint
Recall key_moments about missing ref() causing wrong build order.
Concept Snapshot
ref('model_name') tells dbt which models depend on others.
It builds a dependency graph to run models in correct order.
Always use ref() to link models, not raw table names.
This ensures data freshness and avoids build errors.
Full Transcript
The ref() function in dbt is used to declare dependencies between models. When a model uses ref('other_model'), dbt knows it must build 'other_model' first. This creates a dependency graph that dbt uses to run models in the right order. For example, if 'orders' model uses ref('customers'), dbt builds 'customers' before 'orders'. This prevents errors and ensures data is fresh. If ref() is missing, dbt might build models in the wrong order, causing problems. The execution table shows each step: detecting dependencies, building dependent models, and completing execution. The variable tracker shows how the dependency graph and model statuses change step-by-step. Remember to always use ref() to link models in dbt projects.