In dbt, how does organizing models into subdirectories affect the resulting database schema and table names?
Think about how dbt treats folder names by default when building models.
By default, dbt compiles models into the target schema without creating nested schemas for subdirectories. The folder structure helps organize code but does not affect the database schema names.
Given the following dbt model file path and configuration, what will be the schema and table name of the compiled model?
models/sales/2024/orders.sql
dbt_project.yml contains:
models:
sales:
+schema: sales_dataselect * from raw.ordersCheck how the +schema config applies to subdirectories.
The +schema config under 'sales' applies to all models in the sales folder and its subfolders. The table name is the model file name without folder prefixes.
Consider a dbt project with these model files:
- models/customers.sql
- models/finance/invoices.sql
- models/finance/2024/payments.sql
With no special schema or alias configurations, what are the compiled table names for these models?
Think about how dbt names tables by default from file names.
By default, dbt uses the model file name as the table name, ignoring folder names. So all tables are named after their file names only.
A model 'sales_summary.sql' was moved from models/ to models/sales/. After running dbt, a downstream model referencing it fails with 'model not found'. What is the most likely cause?
Check how dbt's ref() function resolves model names.
dbt's ref() function uses the model name, which is the file name without folder prefixes. Moving a model to a subdirectory does not change its ref() name, so the downstream model's ref() call should remain the same.
You want dbt to create separate schemas in the database that match your model directory names (e.g., models/sales/ creates schema 'sales'). Which configuration achieves this behavior?
Look into how to use Jinja in model configs to customize schema names.
Using the +schema config with a Jinja expression that extracts the directory name from this.path allows dbt to create schemas matching directories dynamically.