Model Contracts and Constraints
dbt Analytics Engineering · Lesson 9 of 17 · 1:09:02
YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗
Learning with these videos? Subscribe for new courses and exam-style questions.
Subscribe on YouTube (opens in a new tab)In this lesson
Prepare for the dbt Analytics Engineering Certification exam. In this lesson: model contracts, the preflight check and breaking changes, constraints declared in YAML, and which constraints each data platform actually enforces. Includes three original practice questions, with Orchardline Meal Kits, a fictional meal-kit company, as the example.
Study notes
The text of this lesson, to read at your own pace. Information as of November 2026.
A column type changed in silence
Orchardline's customers mart feeds the finance dashboard and a partner export. Last week, a developer changed a column from a number to text, and both consumers broke without any warning. This lesson covers model contracts, the preflight check, constraints, and what each platform really enforces.
Model contracts
A model contract guarantees the shape of a model: its column names and data types. You enforce it in YAML, with contract enforced set to true in the model's config. Then every column must be listed, each with a data type that the platform understands. A contract works on table, view, and incremental models, the last with on schema change set to fail, or to append new columns, but never on ephemeral models, materialized views, or Python models. And tests are not part of the contract: the contract fixes the structure, while tests check the quality of the data inside.
The preflight check and breaking changes
When a model with an enforced contract builds, dbt first runs a preflight check: the query must return exactly the columns of the contract, with matching names and data types, in any order. If anything differs, dbt lists the mismatched columns, and the model does not build. dbt then includes the column names, data types, and constraints in the statement it sends to the platform, and orders the columns as the contract does. When you compare with a previous state of the project, dbt also detects breaking changes, like a removed column or a changed data type, and raises a contract error. A breaking change to a contracted model should come with a new model version, as lesson ten explains.
Constraints in YAML
Constraints ask the platform itself to validate the data as it loads: if a row breaks one, the table creation or update fails, and is rolled back. They need an enforced contract, and a table or incremental model. You define them in YAML, under a column: not null, unique, primary key, foreign key, check with an expression, or custom. At the model level, a constraint lists the columns it covers, like a primary key made of two columns. And a foreign key can point to another model with ref, which also adds the dependency to the DAG.
Platform enforcement
Enforcement varies a lot between platforms. Most analytical platforms enforce not null, but keep primary keys, foreign keys, and unique constraints as metadata only, without checking them, while Postgres enforces them all. dbt warns you about a constraint that the platform supports but does not enforce, and you can turn that warning off with warn unenforced set to false. So an unenforced primary key does not stop duplicates: keep unique and not null data tests on that column. A good pattern is to enforce what the platform can, and to test the rest.
Recap
Let's recap. An enforced contract fixes the names and data types of every column, on tables, views, and incremental models. The preflight check stops the build on any mismatch, and dbt flags breaking changes against a previous state. Constraints need an enforced contract, and are defined in YAML, on columns or at the model level. Most platforms enforce only not null, and keep the others as metadata. So keep data tests for whatever the platform does not enforce.
Quick checks for this lesson
The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.
A developer enforces a contract on the customers mart, but lists only the five columns the dashboard uses, out of twelve. What happens when the model builds?
- The build fails, since a contract must list every column
- dbt builds only the five columns listed in it
- dbt builds all twelve, and checks only five of them
- dbt shows a warning, then builds the model anyway
Show the answer
The build fails, since a contract must list every column
A contract covers every column that the model returns, so the preflight check fails while seven columns are missing from it. “dbt builds only the five columns listed in it” is wrong, since a contract never drops columns from the query. “dbt builds all twelve, and checks only five of them” is wrong, since an enforced contract must match the whole query, not part of it. And “dbt shows a warning, then builds the model anyway” is wrong, since a contract mismatch stops the build, instead of only warning.
A developer adds a not_null constraint to the delivery ID of a model materialized as a view, with no contract. What does dbt do with the constraint?
- It adds a not-null check to the view definition
- It runs a not_null data test after the build
- Nothing: it needs a contract and a table or incremental
- It turns the view into a table to apply it
Show the answer
Nothing: it needs a contract and a table or incremental
Constraints only apply when the model enforces a contract, and is a table or an incremental model. “It adds a not-null check to the view definition” is wrong, since constraints are never applied to views. “It runs a not_null data test after the build” is wrong, since a constraint is not a data test, and dbt does not turn one into the other. And “It turns the view into a table to apply it” is wrong, since dbt never changes a model's materialization on its own.
On this warehouse, primary keys are definable but not enforced. The orders mart enforces its contract and declares order_id as primary key. A bug produces duplicate order IDs. What happens?
- The build fails on the primary key constraint
- It builds, and the duplicates stay unless a test fails
- dbt deletes the duplicate rows during the build
- The preflight check fails on the duplicates
Show the answer
It builds, and the duplicates stay unless a test fails
A constraint that is definable but not enforced is metadata only, so only a unique test would catch the duplicates. “The build fails on the primary key constraint” is wrong, since this platform does not check primary keys while loading. “dbt deletes the duplicate rows during the build” is wrong, since dbt never deletes rows on its own. And “The preflight check fails on the duplicates” is wrong, since the preflight check compares column names and types, not values.