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
它的意思
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. (或者 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.
常见原因
- 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';
如何修复它
-
See the row that already holds the key in the message:
SELECT * FROM t WHERE a = 1;
-
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;
-
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.
安全地复现它
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.
问题
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.
相关错误
Constraint ErrorA NOT NULL column received NULLConstraint ErrorA CHECK constraint rejected the rowConversion ErrorThe value cannot be cast to the target type23505PostgreSQL:重复键违反唯一约束SQLITE_CONSTRAINTSQLite:约束失败
代码和消息文本来自 DuckDB documentation。浏览完整的 DuckDB error list 或者 搜索每个数据库错误.
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.
本页文字和数据: CC BY 4.0,请注明出处 SQLly (sqlly.app)。 代码和 SQL 片段: MIT. 许可详情 引用的错误消息文本归其数据库厂商所有。