// 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
類別
Constraint Error
錯誤訊息
Constraint Error: Duplicate key "a: 1" violates primary key constraint.

它的意義

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';

如何修正

  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.

安全地重現問題

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.

相關錯誤

程式碼和訊息文字來自 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. 授權詳情 引用的錯誤訊息文字歸其資料庫廠商所有。