dbt Analytics Engineering (50 Qs)•Part 2: Advanced Modeling, Incremental Builds & Macros (Mid-Level)
What are Model Contracts and why are they vital for Data Contracts?
junior LevelCode ExampledbtAdvanced Modeling
In Simple Words:
Historically, if upstream SQL changed a column from INT to STRING, downstream BI dashboards crashed silently.
Model Contracts (contract: { enforced: true }) enforce strict schema rules on model output:
If the compiled SQL produces unexpected column names, missing columns, or mismatched data types, dbt aborts compilation before writing to the database!
yaml
models:
- name: dim_customers
config:
contract:
enforced: true
columns:
- name: customer_id
data_type: integer
constraints:
- type: not_null
- name: email
data_type: string