Unique test failed: duplicate values in primary key" dbt
Fixes failing dbt unique tests on primary keys by querying the stored failures to list the duplicated values, then deduplicating in the model or fixing the upstream source. Use when a unique test fails on a primary key. Not for unique tests on non-key columns where duplicates may be legitimate.
TL;DR
The unique test found the same primary key value on more than one row. Store the failures, list the duplicated keys, decide whether the fix belongs in the model (dedupe logic) or upstream (source data), apply it, and rerun the test.
Error
"Unique test failed: duplicate values in primary key" dbtSteps
- Re-run with failures stored:
dbt test --select [UNIQUE TEST NAME] --store-failures. Expected: a failures table with the duplicated rows. - Query the failures table grouped by the key column to see which values repeat and how many times. Expected: a short list of duplicated keys.
- Decide where the fix belongs: if the source is wrong, fix upstream; if the model fans out rows (a join multiplying records), fix the model. Expected: a clear owner for the fix.
- For model-side fanout, deduplicate with a window function keeping the right row per key (for example
row_number()partitioned by the key, keeping row 1). Expected: the model emits one row per key. - Re-run
dbt test --select [UNIQUE TEST NAME]. Expected: the test passes.
When to use
- A
uniquetest fails on a column that should be a primary key. - Duplicates appeared after adding or changing a join.
When not to use
- Duplicates are legitimate in the column (then
uniqueis the wrong test; remove it or scope it). - The failure is a
not_nulltest rather thanunique.
Tool compatibility
- dbt Core 1.0 and later, all adapters. Window-function dedupe syntax is standard SQL.
Variant phrasings
Unique test failed on a non-key column
Same technique, but first confirm the column should actually be unique.
Duplicate values only in incremental runs
The incremental logic inserts without checking existing keys; add a merge or dedupe step.
Why it happens
unique compiles to a group-by-having query over the column. Joins that multiply rows, unioned sources with overlap, or genuinely duplicated source data all produce repeated key values.
Edge cases
- Null keys: most warehouses treat nulls as distinct, but some do not; check your warehouse's semantics.
- Case differences (
ABCvsabc) count as distinct on case-sensitive warehouses but may collide elsewhere. - Incremental models need the dedupe inside the incremental branch too, not just the initial build.
Provenance
Resolved from the public thread: https://vectle.com/posts/pst_ujPz0aWZruwB-67BdY-wRA
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.