0
0
DbtHow-ToBeginner ยท 4 min read

How to Organize Models in dbt for Clear Project Structure

In dbt, organize models by grouping SQL files into folders inside the models/ directory to reflect business domains or data layers. Use dbt_project.yml to configure folder-specific settings and apply clear naming conventions for easy navigation and maintenance.
๐Ÿ“

Syntax

dbt organizes models as SQL files inside the models/ directory. You can create subfolders to group related models. The dbt_project.yml file lets you configure these folders with settings like materializations or tags.

Example structure:

  • models/: root folder for models
  • models/staging/: staging models
  • models/marts/: data marts or business layer

In dbt_project.yml, you can specify configurations per folder:

yaml
models:
  staging:
    materialized: view
  marts:
    materialized: table
๐Ÿ’ป

Example

This example shows a simple dbt project structure with two folders: staging and marts. The staging folder contains raw data transformations, and marts contains business logic models.

sql
# Directory structure:
# models/
# โ”œโ”€โ”€ staging/
# โ”‚   โ””โ”€โ”€ customers.sql
# โ””โ”€โ”€ marts/
#     โ””โ”€โ”€ customer_summary.sql

-- models/staging/customers.sql
select * from raw.customers

-- models/marts/customer_summary.sql
select
  customer_id,
  count(*) as order_count
from {{ ref('staging.customers') }}
group by customer_id
โš ๏ธ

Common Pitfalls

Common mistakes when organizing models in dbt include:

  • Putting all models in one folder, making it hard to find or manage them.
  • Not using dbt_project.yml to configure folder-specific settings, leading to inconsistent materializations.
  • Using unclear or inconsistent naming conventions, which confuse collaborators.

Correct organization improves readability and maintainability.

yaml
models:
  # Wrong: no folder config, all models mixed
  
  # Right: separate folders with configs
  staging:
    materialized: view
  marts:
    materialized: table
๐Ÿ“Š

Quick Reference

ConceptDescriptionExample
Folder StructureGroup models by purpose or domainmodels/staging/, models/marts/
dbt_project.ymlConfigure folder-specific settingsmodels: staging: materialized: view
Naming ConventionsUse clear, consistent namesstg_customers.sql, mart_customer_summary.sql
ReferencesUse {{ ref() }} to link models{{ ref('staging.customers') }}
MaterializationsSet how models are built (table/view)materialized: table
โœ…

Key Takeaways

Organize models into folders inside the models/ directory to reflect data layers or domains.
Use dbt_project.yml to set folder-specific configurations like materializations.
Apply clear and consistent naming conventions for easier collaboration.
Use {{ ref() }} to manage dependencies between models cleanly.
Avoid mixing all models in one folder to keep the project maintainable.