dbt test accepted_values failed on nulls
Fixes dbt accepted_values tests that fail on null values by deciding whether nulls are legitimate and either excluding them with a where filter or eliminating them in the model. Use when an accepted_values test fails only because of nulls. Not for failures on unexpected non-null values.
TL;DR
accepted_values treats null as a failure unless you tell it otherwise. If nulls are legitimate, exclude them with a where config on the test; if they are not, fix the model to remove them. Then rerun the test.
Error
dbt test accepted_values failed on nullsSteps
- Store the failures and confirm the failing rows are nulls, not unexpected values:
dbt test --select [TEST NAME] --store-failures. Expected: the failures table shows nulls. - Decide the business rule: are nulls allowed in this column? Expected: a yes-or-no answer from the data owner or the pipeline contract.
- If nulls are allowed, add a
wherefilter to the test config to exclude them:
tests:
- accepted_values:
values: ['a', 'b']
config:
where: "status is not null"Expected: the test now ignores null rows.
- If nulls are not allowed, fix the model: filter them, coalesce to a valid value, or add a
not_nulltest alongside. Expected: the model no longer emits nulls. - Rerun the test. Expected: it passes.
When to use
- An
accepted_valuestest fails and the failing rows are nulls. - You just added the test to a column that historically contains nulls.
When not to use
- The test fails on unexpected non-null values (fix the data or extend the values list).
- Nulls should never occur (add a
not_nulltest and fix the model).
Tool compatibility
- dbt Core 1.0 and later, all adapters.
wheretest configs are adapter-independent.
Variant phrasings
accepted_values fails after a source change introduced nulls
The source contract changed; update the model or the test to match.
Combining acceptedvalues with notnull
The standard pattern: one test for the allowed set, one for nullability, each with clear ownership.
Why it happens
SQL nulls never equal any value, so they never match the accepted list. dbt's test counts them as failures, which surprises teams that think of null as "not applicable" rather than "wrong".
Edge cases
- Empty strings are not nulls; they fail as unexpected values, not as nulls.
- A
wherefilter narrows the test's guarantee; document which rows are excluded. - Changing the model to coalesce nulls alters downstream semantics; prefer the filter when nulls are meaningful.
Provenance
Resolved from the public thread: https://vectle.com/posts/pst_KEYkWovkXf7qBOQrXZlDkg