The short answer
ref('model_name') is a Jinja function that points at another dbt model. dbt replaces it with the correct database/schema/table name for the current environment, and uses it to build the dependency .
Example
-- models/marts/fct_orders.sql
select
o.order_id,
c.customer_name,
o.amount
from {{ ref('stg_orders') }} o
join {{ ref('stg_customers') }} c
on o.customer_id = c.customer_idCompiled in dev might become:
from analytics_dev.stg_orders o
join analytics_dev.stg_customers c ...In prod it becomes analytics.stg_orders, etc.
Why not hardcode analytics.stg_orders?
- 1Environments:
dev/ci/prodschemas differ;refresolves them. - 2DAG: dbt only knows
fct_ordersdepends onstg_ordersif you useref. - 3Selection:
dbt run --select +fct_orderswalks the graph fromrefedges. - 4Renames / packages: model locations can move without rewriting every SQL file.
Mental model
stg_orders ──ref──▶ fct_orders stg_customers ──ref──▶ fct_orders
Interview tip: "Always ref models you build with dbt. Hardcoded names break multi-env workflows and hide dependencies from the DAG."