// DuckDB error reference

Constraint Error: A duplicate key violates a unique constraint

An INSERT or UPDATE tried to store a value that another row already holds in a PRIMARY KEY or UNIQUE column.

Exception
Constraint Error
Category
Constraint Error
Error message
Constraint Error: Duplicate key "a: 1" violates primary key constraint.

What it means

DuckDB keeps indexes for PRIMARY KEY and UNIQUE columns and rejects any write that would create a second row with the same key. The message shows the key as Duplicate key "a: 1" violates primary key constraint. (or unique constraint for a UNIQUE column), so you can see the exact value that collided. Inside an open transaction, when the colliding row was written earlier in that same transaction and not yet committed, DuckDB 2.0.1 reports Constraint Error: PRIMARY KEY or UNIQUE constraint violation: duplicate key "1" instead, which names no column. Run the repro in autocommit mode to see the message shown above.

In multi-row writes the statement fails as a whole. ON CONFLICT DO NOTHING or DO UPDATE makes single-row conflicts resolvable, but DuckDB cannot target several unique constraints in one statement. A DO UPDATE on a table with several UNIQUE or PRIMARY KEY constraints must name its target, or DuckDB returns Binder Error: Conflict target has to be provided for a DO UPDATE operation when the table has multiple UNIQUE/PRIMARY KEY constraints.

Common causes

  • An INSERT with a key that a committed row already has.
  • A replayed load or a retry that inserts the same batch twice without idempotency keys.
  • An UPDATE that moves a key onto a value another row holds.
  • Two unique constraints on one table with an ON CONFLICT clause that cannot cover both.

Find the cause

-- Which keys exist twice in the source you are loading?
SELECT a, count(*) FROM stage GROUP BY a HAVING count(*) > 1;
-- The rows already in the target that the load would hit:
SELECT t.* FROM t JOIN stage s ON t.a = s.a;
-- The constraints on the table:
SELECT constraint_type, constraint_text FROM duckdb_constraints() WHERE table_name = 't';

How to fix it

  1. See the row that already holds the key in the message:

    SELECT * FROM t WHERE a = 1;
  2. Upsert instead of plain INSERT when a replay is possible:

    INSERT INTO t (a) VALUES (1) ON CONFLICT (a) DO UPDATE SET a = EXCLUDED.a;
  3. Deduplicate the source before a bulk load, keeping one row per key:

    INSERT INTO t (a) SELECT a FROM (SELECT *, row_number() OVER (PARTITION BY a ORDER BY id) AS rn FROM stage) WHERE rn = 1;

Other ways to handle it

If the data legitimately contains repeats, the constraint is wrong: drop the PRIMARY KEY or UNIQUE and enforce uniqueness at load time with a deduplication step. Keep the constraint only if the table is meant to hold one row per key.

Reproduce it safely

CREATE TABLE t(a INT PRIMARY KEY);
INSERT INTO t VALUES (1);
INSERT INTO t VALUES (1);   -- Constraint Error: Duplicate key "a: 1" violates primary key constraint.

Questions

Can I catch the conflict and continue with the next row?

Not inside one statement: the statement fails as a whole. Use ON CONFLICT for known keys, or load into a staging table, keep one row per key (SELECT DISTINCT, or the row_number() step in the fix above), and insert with a WHERE NOT EXISTS against the target. A staging table with repeated keys fails the WHERE NOT EXISTS insert too.

Why does DuckDB say primary key when my column is UNIQUE?

Read the end of the message: it says unique constraint for UNIQUE columns and primary key constraint for PRIMARY KEY columns.

Related errors

The code and message text are from the DuckDB documentation. Browse the full DuckDB error list or search every database error.

// fewer errors, faster fixes

Work on DuckDB in SQLly

A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for DuckDB and the other databases you use.

Text and data on this page: CC BY 4.0, credit SQLly (sqlly.app). Code and SQL snippets: MIT. License details Quoted error message text belongs to its database vendor. View as Markdown