Skip to content

Models

Course map · Previous: 5. Set up the project · Next: Sources

Staging models rename raw columns; marts join them with ref(). dim_customers builds on fct_orders, which builds on two staging models.

Open Run (from oms_dbt_proj)
models/staging/, models/marts/ dbt run --profiles-dir .

The jaffle_shop DAG: sources, staging models, fct_orders and dim_customers

To trace lineage inside one model, CTE by CTE, use QLineage. To see every table and how the keys join, open the ERD.

New to models, ref() or the DAG? Read 4. How dbt builds tables first.

Learn

  • A model is one select in a .sql file. dbt builds it as a view or a table: dbt_project.yml sets that per folder, and {{ config(materialized='table') }} at the top of a model overrides it.
  • If the view or table already exists, dbt replaces it. The exact SQL dbt ran is in target/run/.
  • models/staging/ holds one cleaning model per raw table. models/marts/ holds the business tables, fct_orders and dim_customers, built only with ref().

Run it in the browser

A model is one select. stg_jaffle_shop__customers renames id to customer_id, and dbt compiles its {{ source('jaffle_shop', 'customers') }} to raw.jaffle_shop.customers. This is the SQL dbt runs for it:

select id as customer_id, first_name, last_name
from raw.jaffle_shop.customers

dim_customers counts each customer's orders. Change the query below to count orders per customer: group by user_id and add count(*) as number_of_orders. 62 customers have orders.

select user_id as customer_id
from raw.jaffle_shop.orders

Quiz

In this project, what does dbt run build for stg_jaffle_shop__customers?

  • A table, because every model is a table
  • A view, because dbt_project.yml sets +materialized: view on the staging folder
  • Nothing, because staging models only compile
Answer

A view. Materializations are set per folder in dbt_project.yml: staging builds views, marts build tables. A {{ config(materialized='table') }} block in a model overrides the folder setting.

Why does fct_orders select from {{ ref('stg_jaffle_shop__orders') }} instead of writing the table name?

  • ref() makes the query run faster
  • Writing the table name is a syntax error in dbt
  • dbt reads ref() to learn the dependency, builds models in the right order, and fills in the right schema for each target
Answer

Dependency, order and target. Every ref() is an edge in the DAG. A hard-coded name still runs, but dbt can't see the dependency, and the name would be wrong on the other target.

Where should the join of orders and payments live?

  • In a mart, such as fct_orders
  • In stg_stripe__payments, next to the payment data
  • In the source YAML
Answer

In a mart. Staging models map one-to-one to source tables and only rename and cast. Joins and business logic go in marts, built with ref() on staging models.

Try it

Add a full_name column (first name, a space, last name) to dim_customers, then run only that model.

Hint

Add the column to the final CTE in models/marts/marketing/dim_customers.sql. || joins strings in both DuckDB and Snowflake. -s dim_customers runs one model.

Answer

In models/marts/marketing/dim_customers.sql, add this line to the final CTE, under customers.last_name,:

        customers.first_name || ' ' || customers.last_name as full_name,
dbt run --profiles-dir . -s dim_customers
dbt show --profiles-dir . -s dim_customers --limit 2

The output has a full_name column, for example Michael P..