SQLITE_CONSTRAINT_PRIMARYKEY: PRIMARY KEY constraint failed
An INSERT or UPDATE would duplicate a primary key value. SQLite's message still says UNIQUE constraint failed and names the key column.
- Result code
SQLITE_CONSTRAINT_PRIMARYKEY- Name
1555- Category
- Extended result codes
What it means
SQLITE_CONSTRAINT_PRIMARYKEY (1555) is the extended code for a duplicate primary key. The message text is the same as for any unique violation, UNIQUE constraint failed: table.column, so the extended code is how a program tells the two apart.
For an INTEGER PRIMARY KEY column you can leave the value out (or pass NULL) and SQLite assigns the next rowid, which avoids most collisions.
Common causes
- Inserting an explicit id that already exists.
- Copying rows between databases with their ids, where the ranges overlap.
- Generating ids in the application with a scheme that can repeat.
How to fix it
-
Let SQLite assign
INTEGER PRIMARY KEYvalues by leaving the column out.INSERT INTO orders (customer_id, total) VALUES (42, 19.99); SELECT last_insert_rowid();
-
When merging data, upsert on the key or skip existing rows.
INSERT INTO orders (id, customer_id, total) SELECT id, customer_id, total FROM other.orders WHERE true ON CONFLICT (id) DO NOTHING;
Related errors
SQLITE_CONSTRAINT_UNIQUEUNIQUE constraint failedSQLITE_MISMATCHdatatype mismatchSQLITE_CONSTRAINTconstraint failed23505PostgreSQL: duplicate key violates unique constraint1062MySQL: Duplicate entry for key
More extended result codes errors
SQLITE_ERROR_MISSING_COLLSEQno such collation sequenceSQLITE_BUSY_RECOVERYanother process is recovering the WALSQLITE_LOCKED_SHAREDCACHErecord locked in shared cacheSQLITE_CONSTRAINT_CHECKCHECK constraint failedSQLITE_ABORT_ROLLBACKstatement aborted by ROLLBACKSQLITE_BUSY_SNAPSHOTstale read can't become a writeSQLITE_IOERR_SHORT_READread returned fewer bytesSQLITE_CANTOPEN_ISDIRthe path is a directory
The code and message text are from the SQLite result and error codes documentation. Browse the full SQLite error list or search every database error.
Work on SQLite in SQLly
A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for SQLite and the other databases you use.