guide
guide8 min read

Why Does dbt CI Fail on Models I Didn't Change, and How Do I Scope It to Modified Models?

Build only what the pull request touched, defer the rest to production, and keep unrelated failures out of the check

dbt CI fails on models you didn't change because the job selects more than your diff: it either runs the whole project, or it runs state:modified+, which by definition includes every downstream model and test, or it compares against a stale or mismatched manifest and flags nodes that did not really change. The fix documented in the dbt best-practice workflows is slim CI: select state:modified+ and pass --defer --state pointing at production artifacts. This guide walks through each cause and the scoping that removes it.

Key Takeaways

  • •A CI job that runs plain dbt build tests the whole project, so any pre-existing failure anywhere blocks every pull request.
  • •state:modified+ selects changed nodes plus everything downstream, so a failing downstream model you never touched can still fail your check.
  • •--defer --state <path> lets unbuilt upstream models resolve to production relations, so CI does not need to rebuild the whole graph.
  • •Spurious state:modified hits usually come from a stale manifest, a different dbt version, or environment-dependent config, not from your diff.
  • •Separate 'my change broke it' from 'it was already broken' by comparing the failing node against the production run before you fix anything.

Why does a pull request check fail on models that aren't in the diff?

The dbt Discourse thread this question comes from describes the common shape: a developer edits one model, opens a pull request, and the check goes red on models in a different part of the project. Nothing about the failure is strange. The CI job is doing exactly what its selector says, and the selector is wider than the diff.

There are four usual reasons. The job runs dbt build with no selector, so all models, tests, snapshots and seeds execute. The job selects state:modified+, which pulls in downstream nodes whose data or tests were already failing. The comparison manifest is out of date, so dbt believes models changed that did not. Or an upstream model is missing from the CI schema, so a downstream model errors on a relation that does not exist.

Each cause has a different fix, so it is worth identifying which one you have before changing the job. Open the failing check, read the first error, and note whether it is a compilation error, a missing relation, or a test failure. Those three symptoms map to the sections below.

How does state:modified decide what changed?

The state selector method compares your current project against a previous manifest that you supply with --state. The dbt node selection methods reference lists the sub-selectors: state:modified.body for SQL changes, state:modified.configs for config changes, state:modified.macros for changes to macros a node depends on, plus state:new for nodes absent from the old manifest. Plain state:modified matches any of them.

That comparison is only as good as the manifest you give it. If the artifacts came from a run on a different dbt version, a different target, or a different set of environment variables, configs can differ even though no one edited a file. A model whose config reads env_var() or a target-specific variable is a typical source of phantom changes, and the model then shows up in the selection and fails for reasons unrelated to your pull request.

A quick diagnostic is to run dbt ls --select state:modified --state <path> on a clean branch with no edits. The output should be empty. Every node it lists is a false positive, and the sub-selectors (state:modified.body versus state:modified.configs) tell you which kind of difference dbt is seeing.

What is the slim CI command that scopes the job?

The dbt continuous integration docs describe running only modified resources and their dependents. In a script-driven CI job the equivalent is a single command: dbt build --select state:modified+ --defer --state ./prod-artifacts. The artifacts directory holds the manifest.json from your latest successful production run, downloaded at the start of the job.

The + matters. state:modified alone runs only the nodes you edited, which can miss a downstream break. state:modified+ includes children so you learn whether your change breaks a model that reads from it. The cost is that children with pre-existing problems now sit inside your selection, which is the second cause in the first section.

If you want a narrower first pass, run state:modified first and state:modified+ second, or add --exclude for known-flaky nodes while their owners fix them. Treat exclusions as a visible, time-boxed list in the repo, not a silent workaround, because they quietly reduce what CI protects.

What does --defer do, and why does CI need it?

With a selector like state:modified+, CI builds only some of the graph. Any selected model that refs an unbuilt upstream model needs somewhere to read it from. The dbt defer reference explains that --defer resolves those ref() calls to the relations in the comparison manifest, which means production tables, instead of failing on a missing relation in the CI schema.

Without --defer, you see errors such as a relation not existing for a model you never touched. That error is not a data problem. It is a missing build, and it disappears once deferral is on. If you still hit it, confirm that the --state path points at the production manifest and that the production relations exist in a schema your CI role can read.

The docs also describe --favor-state, which prefers the production relation even when a development relation of the same name exists. Use it when a stale CI schema from an earlier run is shadowing production and returning old data.

How do you tell your failure from a pre-existing one?

Scoping removes unrelated builds, but a downstream test can still fail for reasons older than your change. The fastest check is to look up the same node in the latest production run. If the test also failed or warned there, the problem pre-dates your pull request and belongs in its own ticket, not in your diff.

If production was green, compare the node's output between the production relation and the CI build. A changed row count or a new null in a column your model feeds is evidence that your change caused it. Contracts and unit tests help here because they fail on shape and logic respectively, which narrows the cause before anyone reads query output.

Write the answer down in the pull request: 'failing in prod since date, unrelated' or 'introduced by this change in column X'. Reviewers then decide on facts, and the next developer who hits the same red check finds the reasoning instead of re-deriving it.

Where can an agent help without touching production?

The triage above is repetitive: list modified nodes, diff against the production manifest, find the first error, check production history, and write the verdict. The Pipeline Agent can run those steps and post the result on the pull request. It works read-only by default, so it reports whether a failing node is in your blast radius, a false positive from state comparison, or a pre-existing failure.

Agents reach tools through MCP, and the MCP specification in its 2025-06-18 revision defines how a client discovers and calls them. That means the same triage can be invoked from Claude Code or Cursor in your terminal with a question like 'what's the blast radius if I change this model?', with no new dashboard to open.

We keep the verdict rule-based and use the model to gather and explain evidence. Fixes go through a human-approved pull request, and a per-domain autonomy dial sets how far an agent may go on its own. For CI, most teams start at report-only.

A checklist for scoping your dbt CI job

  • •Publish manifest.json from every successful production run to storage your CI job can read.
  • •Download it at job start and run dbt build --select state:modified+ --defer --state <path>.
  • •Pin the same dbt version in CI and production so config comparisons do not drift.
  • •Run dbt ls --select state:modified on a clean branch; a non-empty result means false positives to fix first.
  • •Keep any --exclude list short, dated and owned.
  • •Record whether a failing node was already failing in production before you change code.

Teams that adopt this usually see the same pattern: the red checks that remain are the ones the pull request earned. The rest become a visible backlog of existing data issues with owners, which is a better place for them than a developer's merge queue. The dbt-core repository tracks changes to state selection and deferral behaviour, so read its release notes when you upgrade.

If your project mixes contracts, data tests and unit tests, the same scoping applies to all three, because dbt build runs them together under one selector. See our comparison of model contracts and data tests for what each one catches in CI.

Frequently Asked Questions

Why does state:modified flag models I never edited?

Usually the comparison manifest differs from your branch in something other than your edit: a different dbt version, a different target, a changed macro, or a config that reads an environment variable. Run dbt ls --select state:modified --state <path> on an unedited branch. Anything listed is a false positive, and state:modified.body, .configs and .macros show which kind of difference dbt detects. Regenerate the production manifest with the pinned dbt version if they disagree.

Do I need state:modified+ or is state:modified enough?

Use state:modified+ when you want CI to catch downstream breakage, which is the point of testing a change to a shared model. Use state:modified alone only for a fast first pass or when downstream models are owned and tested elsewhere. The trade-off is that + brings in children that may already be failing, so pair it with a production-history check before blaming the pull request.

Why do I get a missing relation error for an upstream model?

CI built only the selected models, and a selected model refs an upstream one that does not exist in the CI schema. Add --defer --state <path> so the ref resolves to the production relation. If the error persists, check that the path holds the production manifest and that the CI role can read the production schema. --favor-state helps when an old CI relation shadows the production one.