0
0
dbtdata~10 mins

Organizing models in directories in dbt - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Organizing models in directories
Create root models folder
Create subdirectories for model groups
Place SQL model files in subdirectories
dbt reads directory structure
Models are organized logically
Run dbt to build models
Output models reflect directory organization
Organize your dbt SQL models into folders to keep related models together, making your project easier to manage and understand.
Execution Sample
dbt
models/
  sales/
    sales_summary.sql
    sales_details.sql
  marketing/
    campaign_performance.sql
    leads.sql
This folder structure groups sales and marketing models into separate directories under the main models folder.
Execution Table
StepActionDirectory StructureModel Files Founddbt Behavior
1Start with empty models foldermodels/NoneNo models to build
2Create sales directorymodels/sales/NoneNo models yet in sales
3Add sales_summary.sqlmodels/sales/sales_summary.sqldbt detects sales_summary model
4Add sales_details.sqlmodels/sales/sales_summary.sql, sales_details.sqldbt detects both sales models
5Create marketing directorymodels/marketing/NoneNo models yet in marketing
6Add campaign_performance.sqlmodels/marketing/campaign_performance.sqldbt detects campaign_performance model
7Add leads.sqlmodels/marketing/campaign_performance.sql, leads.sqldbt detects both marketing models
8Run dbt buildmodels/sales/, models/marketing/All 4 modelsdbt builds all models organized by directory
💡 All model files are detected and built by dbt according to their directory organization.
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 6After Step 7Final
models folder contentemptysales/sales/ with 1 filesales/ with 1 file, marketing/sales/ with 1 file, marketing/ with 2 filessales/ with 2 files, marketing/ with 2 files
dbt detected modelsnonesales_summarysales_summary, sales_detailssales_summary, sales_detailssales_summary, sales_details, campaign_performance, leadssales_summary, sales_details, campaign_performance, leads
Key Moments - 2 Insights
Why does dbt detect models inside subdirectories automatically?
dbt scans all folders under the main models directory recursively, so any SQL files inside subfolders are included as models, as shown in execution_table steps 3, 4, 6, and 7.
Can I organize models in any folder structure I want?
Yes, as long as the folders are inside the main models directory, dbt will find all SQL files. This helps keep related models grouped logically, as seen in the variable_tracker showing folder contents.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, how many models does dbt detect?
A2
B1
C3
D0
💡 Hint
Check the 'Model Files Found' column at step 4 in the execution_table.
At which step does the marketing directory first contain model files?
AStep 7
BStep 5
CStep 6
DStep 4
💡 Hint
Look at the 'Directory Structure' and 'Model Files Found' columns for marketing folder in execution_table.
If you add a new folder 'finance' with models inside, what will dbt do?
AIgnore the finance folder
BDetect models inside finance folder automatically
COnly detect models in root models folder
DRequire special config to detect finance models
💡 Hint
Refer to key_moments about dbt scanning all subdirectories under models.
Concept Snapshot
Organizing models in directories:
- Place SQL files inside folders under 'models/'
- dbt scans all subfolders recursively
- Keeps project tidy and logical
- No extra config needed
- Models built reflect folder structure
Full Transcript
This visual execution shows how organizing dbt models in directories works. We start with an empty models folder. Then we create subfolders like sales and marketing. We add SQL model files inside these folders. dbt automatically detects all models inside these directories when it runs. The execution table traces each step of adding folders and files, showing how dbt finds models. The variable tracker shows how folder contents and detected models grow over time. Key moments clarify that dbt scans all subdirectories and that any folder under models is included. The quiz tests understanding of when models are detected and how dbt handles new folders. The snapshot summarizes the key points for quick reference.