What is the primary purpose of a model contract in dbt?
Think about how contracts help maintain data quality and structure.
Model contracts specify the expected schema of a model, helping to enforce data consistency and catch schema changes early.
Given a dbt model with a contract specifying columns id INT and name STRING, what will be the result if the model returns an extra column age INT?
version: 2
models:
- name: customers
columns:
- name: id
tests:
- not_null
- name: name
tests:
- not_null
contract: trueContracts enforce exact schema matching, including no extra columns.
Model contracts require the model output to exactly match the defined schema. Extra columns cause a failure.
Consider a dbt project where access controls restrict the finance schema to only the finance_team role. If a user without this role runs a model targeting the finance schema, what will be the outcome?
Think about how database permissions affect write operations.
Without the proper role, the user cannot write to the restricted schema, causing a permission error.
A dbt model contract test fails with the error: Column 'id' missing in model output. The model SQL is:
select id as customer_id, name from customers
Why does the contract test fail?
Check if the contract column names match the model output exactly.
The contract expects a column named 'id' but the model renames it to 'customer_id', causing a mismatch.
You manage a dbt project with multiple teams: sales, marketing, and finance. Each team should only access their own schema data. How should you configure model contracts and access controls to enforce this?
Think about combining schema-level permissions with contracts for data integrity.
Defining contracts per schema ensures data structure consistency, and role-based permissions restrict access properly.