dbt Model Contracts vs dbt Tests: Which One Catches Breaking Changes in PR CI?
Contracts guard the interface, tests guard the values, and your CI needs both in the right order
A dbt model contract checks a model's interface (column names, data types and declared constraints) against the model's SELECT before dbt writes the table, while a dbt test checks values and logic. The dbt model contracts docs say dbt runs a "preflight" check that the query returns columns whose names and data types match the contract, "or it will fail to build." Tests answer a different question: is the data in the table right? Use contracts for the shape, tests for the content.
Key Takeaways
- •A contract fails a model whose column names or data types differ from its YAML, and column order does not matter, per the dbt model contracts docs.
- •Data tests and unit tests check values and logic, so a renamed or retyped column that still produces plausible rows can slip past them.
- •Contract enforcement and breaking-change detection are two mechanisms: the first runs against the warehouse when the model builds, the second compares your project to a previous state.
- •Unit tests arrived in dbt v1.8, and dbt Labs recommends running them in development and CI only, not in production.
- •Constraint enforcement depends on the platform, so a contract with a unique or primary_key constraint can pass on a warehouse that only records the constraint.
What a contract actually checks
A contract is a promise about a model's output schema. You set contract: enforced: true in the model's config, then list every column with a name and data_type. The contract config reference lists three checks: name, data type and the number of columns. If the model's SELECT disagrees with the YAML, dbt raises a compilation error that names the column and the mismatch reason, such as "data type mismatch", and the table is not written.
Two limits are worth knowing before you rely on it. First, the same reference says size, precision and scale are not compared, so varchar(256) and varchar(257) count as equivalent. Second, contracts apply to table and incremental models (incremental ones need on_schema_change set to append_new_columns or fail) and to views for names and types only. Python models, materialized views and ephemeral models are out.
This is why we treat a contract as an interface test. It does not look at a single row. It asks whether what you are about to build still has the shape your downstream consumers were promised. A 2026 write-up on dev.to puts it as "tests are about values, contracts are about interfaces," and we agree with the split.
What dbt tests check, and why they miss interface changes
Data tests (not_null, unique, accepted_values, relationships and custom SQL) run after the model is built and query the resulting table. They are the right tool for facts about values: a key that duplicates, a status column with an unexpected value, a foreign key with orphans. They run on real data, so they find what fixtures cannot.
They are weaker at interface drift. If an engineer renames customer_id to cust_id, a not_null test on customer_id errors because the column is gone, but nothing tells the author that a downstream model, dashboard or reverse-ETL sync depended on the old name. If a column silently changes from integer to string, no value test fails unless you wrote one for it. The table builds, the rows look fine, and the break shows up in a consumer.
Unit tests sit between the two. Per the dbt unit tests docs, they validate your SQL modeling logic "on a small set of static inputs before you materialize your full model," and they are available from dbt v1.8. They catch logic regressions, such as a changed CASE branch, using fixtures you control. They do not tell you what production data looks like, and they do not guard the schema your consumers read.
Contracts vs tests: what each catches in a PR
The table below maps common pull-request changes to the check that is built to catch them. "Built to catch" matters: a test can sometimes catch a change by accident, but we only mark a cell when the mechanism is designed for it.
| Change in the PR | Contract | Data test | Unit test |
|---|---|---|---|
| Column renamed or dropped in the SELECT | Yes, name mismatch fails the build | Only if a test references that column, and then as a missing-column error | Only if the expected output lists the old name |
| Column type changed (int to string) | Yes, data type mismatch | No, unless you wrote a type-sensitive test | Only if fixture types expose it |
| New nulls appear in a key | Only where the platform enforces a not_null constraint | Yes | Only for the fixtures you wrote |
| Duplicate keys appear | No, unique is not enforced on every platform | Yes | Only for fixtures |
| Revenue formula changed incorrectly | No | Only if a reconciliation test exists | Yes, this is the target case |
| Breaking change vs the last production state | Yes, via state comparison (see below) | No | No |
Read the last row twice. Catching a change against the previous deployed state is a separate mechanism from enforcing the YAML against the SELECT, and the next section keeps them apart.
Two mechanisms people blur into one
The first mechanism is enforcement. When a contracted model builds, dbt compares the columns its query would return against the YAML and fails before it writes the table. That needs a connection to the warehouse and happens as part of the run, not as a static parse. Treat it as a build-time check on the warehouse, not a free lint step.
The second is breaking-change detection. The same docs page says that "when comparing to a previous project state, dbt will look for breaking changes that could impact downstream consumers." The listed breaking changes are removing a column, changing a column's data_type, and removing or modifying a constraint (dbt v1.6 or higher). From dbt v1.9, removing a contracted model also counts. This check compares two project definitions, so it needs the production manifest as a reference. The docs page does not spell out the exact flag or timing, so confirm on your dbt version which phase raises it.
Why separate them? Because they fail for different reasons and at different cost. Enforcement tells you the new SELECT does not match its own YAML. Detection tells you the YAML itself changed in a way that breaks consumers, even when the SELECT and YAML agree. A rename done carefully in both places passes enforcement and still fails detection. A sloppy rename in only the SQL fails enforcement. You want both signals in a PR.
A CI order that keeps the two apart
Here is the order we would use. It is built around one idea: run the checks that need no production data first, the checks that need the warehouse next, and the checks on real values last.
- •Step 1, state compare: fetch the production manifest into
./prod-artifactsso dbt has a previous project state to compare against. - •Step 2, unit tests on changed models:
dbt test --select "state:modified,test_type:unit" --state ./prod-artifacts. These use static fixtures and cost almost nothing in compute. - •Step 3, build with contracts:
dbt build --select state:modified+ --defer --state ./prod-artifacts. Contract enforcement runs as each contracted model builds, and a mismatch stops it before the table is written. - •Step 4, data tests:
buildruns them after each model, so a duplicate key or a null spike fails the PR on real rows. - •Step 5, report: post the failing column, model and downstream consumers back to the pull request.
The state:modified+ selector builds changed models and everything downstream, so a contract failure in a parent stops its children from building. Run this against a dedicated CI schema, not production. dbt Labs also says in the unit tests docs to run unit tests in development or CI only and to exclude them from production builds with --exclude-resource-type to save compute.
A minimal contract to start from looks like this:
- •
models/marts/dim_customers.yml: setconfig: contract: enforced: trueon the model. - •List
customer_idasdata_type: intwith anot_nullconstraint, andcustomer_nameasdata_type: string. - •Add a
uniquedata test oncustomer_id, because a unique constraint may only be recorded, not enforced, on your warehouse.
Platform differences you have to plan around
Name and type checks are the same everywhere, but constraints are not. The constraints reference has a per-platform support table, and the pattern in it is that not_null is the constraint most platforms enforce, while primary_key, foreign_key and unique are often "definable only", meaning dbt writes them to the DDL but the warehouse does not stop duplicates. The same page notes that where a constraint is not supported, dbt skips it with a warning.
The practical rule: a contract guarantees the interface on every platform, and guarantees uniqueness only where your platform enforces it. Keep a data test on every key you care about, even when you also declare a constraint. The contract documents intent for consumers, and the test checks the data.
For incremental models, the contract docs add a trap. If you add a column to the YAML but the existing table lacks it and on_schema_change is not set, the YAML and the real table can disagree. They also advise against sync_all_columns on contracted models, because removing a column is a breaking change. Pick append_new_columns or fail deliberately.
Where agents fit into contract and test review
Everything above runs with plain dbt. The part that stays manual is the review: someone reads the contract error, finds which downstream models use the column, and decides whether to version the model or revert. Our Schema Agent works on that step, detecting column-level drift and projecting which downstream assets a change touches. We built it to run alongside dbt's own checks, not to replace them.
We run these agents as MCP servers inside Claude Code and Cursor, so an engineer can ask "what's the blast radius if I drop this column?" while reviewing the PR. The OSS edition is read-only by default, so it reports impact and does not edit your models. Anything that writes goes through approval in Pro. The dbt side is open source too: contracts live in dbt-core on GitHub, which is Apache 2.0 licensed, so you can read exactly what each check does.
We also publish a related piece on how schema evolution, schema drift and data contracts differ, titled "schema evolution vs schema drift vs data contracts: which stops pipeline breakage?" Contracts are the enforcement layer in that framing. Tests are the audit layer.
Frequently Asked Questions
Do dbt model contracts replace dbt tests?
No. A contract checks column names, data types and the number of columns for the model's output, and it never inspects a row. You still need data tests for duplicates, orphans, accepted values and reconciliation, and unit tests for logic. The dev.to article we cited recommends contracts as the first line of defense and tests as the audit layer, and that matches how we set up CI.
Does a dbt contract run before or after the model builds?
Before the table is written, but as part of the run. dbt performs a preflight check on the model's query and fails the build on a mismatch, so you get no table with the wrong shape. Detection of breaking changes against a previous state is a separate comparison. Because the docs do not state exactly when that comparison fires, test it on a throwaway branch with your dbt version before you design CI around it.
Which dbt version do I need for contracts and unit tests?
Contracts need dbt 1.5 or later, and constraint-change detection needs 1.6 or later. Removed-model detection needs 1.9. Unit tests need v1.8, per the dbt docs. If you are on an older version, upgrade first, because you will otherwise be hand-writing checks that the framework now does for you.