# 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.

Source: https://sqlly.app/errors/duckdb/constraint-unique
Last updated: 2026-11-30
License: Text and data [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/) (credit SQLly, sqlly.app); code and SQL snippets [MIT](https://opensource.org/license/mit). [Details](https://sqlly.app/license).
Note: Quoted error message text belongs to its database vendor.

- Exception: `Constraint Error`
- Category: Constraint Error

Error message:

```text
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.

## How to fix it

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

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

   ```sql
   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:

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

## Reproduce it safely

```sql
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

- [`Constraint Error`](https://sqlly.app/errors/duckdb/constraint-not-null): A NOT NULL column received NULL
- [`Constraint Error`](https://sqlly.app/errors/duckdb/constraint-check): A CHECK constraint rejected the row
- [`Conversion Error`](https://sqlly.app/errors/duckdb/conversion-could-not-convert): The value cannot be cast to the target type
- [`23505`](https://sqlly.app/errors/postgresql/23505): PostgreSQL: duplicate key violates unique constraint
- [`SQLITE_CONSTRAINT`](https://sqlly.app/errors/sqlite/sqlite-constraint): SQLite: constraint failed

The code and message text are from the [DuckDB documentation](https://duckdb.org/docs/).
