Skip to content
Latchkey

dbt not_null / relationships test failure in CI

A dbt not_null test found nulls in a column that must be populated, or a relationships test found values with no matching parent key. Both fail because dbt is configured to fail when the offending row count is not zero.

What this error means

A dbt test step fails with "Failure in test not_null_<model>_<column>" or "relationships_<model>_<column>" and "Got N results, configured to fail if != 0".

dbt
Failure in test relationships_orders_customer_id__id__ref_customers_
  Got 23 results, configured to fail if != 0
  23 orders reference a customer_id that does not exist in customers

Common causes

Missing or orphaned values

A not_null column has nulls, or a relationships foreign key points at rows absent from the parent, usually from incomplete upstream data.

Tests ran before dependencies were built

The referenced parent model was not built in CI, so every child row looks orphaned.

How to fix it

Build dependencies then test

  1. Use dbt build so parents are materialized before relationship tests run.
  2. Inspect failing rows with --store-failures.
  3. Fix the upstream data or the join producing nulls or orphans.
Terminal
dbt build --select +orders   # build parents, then run its tests

Correct the constraint if it is too strict

If nulls are legitimately allowed, drop not_null; if orphans are expected during a window, relax the relationships test or filter it.

schema.yml
- relationships:
    to: ref('customers')
    field: id
    config:
      where: "created_at < current_date"

How to prevent it

  • Prefer dbt build so tests run after their dependencies.
  • Store failures to inspect the exact offending rows.
  • Keep constraints honest to the real data contract.

Frequently asked questions

What causes "dbt not_null / relationships failure"?
A not_null column has nulls, or a relationships foreign key points at rows absent from the parent, usually from incomplete upstream data.
How do I fix dbt not_null / relationships failure?
Build dependencies then test

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card