Organizing models in directories helps keep your project neat and easy to understand. It makes finding and managing your data models faster.
Organizing models in directories in dbt
Start learning this pattern below
Jump into concepts and practice - no test required
models/
├── staging/
│ ├── customers.sql
│ └── orders.sql
├── marts/
│ ├── sales.sql
│ └── finance.sql
└── intermediate/
└── customer_orders.sqlEach folder inside models/ is a directory for related models.
dbt automatically finds models in subdirectories without extra config.
models/staging/customers.sql models/staging/orders.sql models/marts/sales.sql
models/finance/revenue.sql models/finance/expenses.sql models/marketing/campaigns.sql
models/2024_Q1/sales.sql models/2024_Q1/returns.sql models/2024_Q2/sales.sql
This example shows two models in different folders. The customer_summary.sql model uses ref() to connect to models in the staging folder.
/* File: models/staging/customers.sql */ select * from raw.customers /* File: models/marts/customer_summary.sql */ select customer_id, count(order_id) as total_orders from {{ ref('customers') }} join {{ ref('orders') }} using (customer_id) group by customer_id
Use meaningful folder names to make your project easy to navigate.
dbt runs models in dependency order using ref(), so folder structure does not affect build order.
You can add dbt_project.yml configs to control model materializations per folder.
Organizing models in directories keeps your dbt project clean and easy to manage.
Folders help group models by function, team, or time period.
dbt automatically detects models in subfolders and builds them correctly.
Practice
Solution
Step 1: Understand project organization benefits
Organizing files into folders helps keep things tidy and easy to find.Step 2: Relate to dbt model management
In dbt, directories group models logically, making the project easier to manage.Final Answer:
It keeps the project clean and easier to manage. -> Option AQuick Check:
Organizing models = easier management [OK]
- Thinking folders speed up dbt runs
- Believing folders fix SQL errors
- Assuming folders create dashboards automatically
Solution
Step 1: Understand dbt model referencing
dbt uses dot notation to reference models in subfolders: folder.model_name.Step 2: Check each option
Only SELECT * FROM subfolder.model_name uses correct dot notation with folder before model name.Final Answer:
SELECT * FROM subfolder.model_name -> Option CQuick Check:
Use folder.model_name to reference subfolder models [OK]
- Reversing folder and model name order
- Using hyphens instead of dots
- Ignoring folder name when referencing
models/
sales/
orders.sql
marketing/
campaigns.sqlWhat is the correct way to select all columns from the orders model inside the sales folder?
Solution
Step 1: Identify model location
The orders model is inside the sales folder under models.Step 2: Use correct reference syntax
dbt references models using folder.model_name, so sales.orders is correct.Final Answer:
SELECT * FROM sales.orders -> Option BQuick Check:
Reference subfolder models as folder.model_name [OK]
- Omitting folder name when selecting
- Using full path with 'models.' prefix
- Selecting from wrong folder model
models/finance/revenue.sql. You wrote this SQL:SELECT * FROM revenue
Why might this cause an error when running dbt?
Solution
Step 1: Understand model referencing in subfolders
Models inside subfolders must be referenced with folder prefix, e.g., finance.revenue.Step 2: Identify cause of error
Using just revenue ignores the folder, causing dbt to not find the model.Final Answer:
Because the model should be referenced as finance.revenue. -> Option AQuick Check:
Reference subfolder models with folder prefix [OK]
- Assuming model name alone works in subfolders
- Thinking SQL syntax is wrong
- Believing dbt disallows subfolders
Solution
Step 1: Understand multi-level directory organization
Organizing by year then quarter creates clear hierarchy: year_2024/q1/.Step 2: Check referencing simplicity
dbt references nested folders with dot notation: year_2024.q1.sales is clear and logical.Final Answer:
models/year_2024/q1/sales.sql and reference as year_2024.q1.sales -> Option DQuick Check:
Use nested folders with dot notation references [OK]
- Reversing folder order causing confusing references
- Using flat filenames losing hierarchy clarity
- Ignoring dot notation for nested folders
