dbt test "Failure ... got N results, expected 0" (unique) in CI
A dbt unique test found duplicate values in a column that is supposed to be unique. dbt reports the count of offending rows and fails because it is configured to fail when the result is not zero.
What this error means
A dbt test step fails with "Failure in test unique_<model>_<column> ... Got N results, configured to fail if != 0" for one or more models.
Failure in test unique_orders_order_id (models/marts/orders.yml)
Got 7 results, configured to fail if != 0
compiled Code at target/compiled/.../unique_orders_order_id.sqlCommon causes
Duplicate keys in the model output
The transform produced repeated values in a column declared unique, often from a fan-out join or a missing dedup step.
The uniqueness assumption is wrong
The column is unique only in combination with another; a single-column unique test is the wrong constraint.
How to fix it
Inspect the failing rows and dedup
- Open the compiled test SQL that dbt printed and run it to see the duplicates.
- Fix the join or add a dedup so keys are unique.
- Re-run
dbt buildso the model is rebuilt before the test.
dbt test --select unique_orders_order_id --store-failuresUse a combination uniqueness test
If uniqueness is over multiple columns, use dbt_utils.unique_combination_of_columns instead of a single-column test.
- dbt_utils.unique_combination_of_columns:
combination_of_columns: [order_id, line_number]How to prevent it
- Run
dbt buildso models rebuild before tests in CI. - Use
--store-failuresto inspect offending rows. - Model composite keys with combination-uniqueness tests.