SQLITE_CONSTRAINT_PRIMARYKEY
SQLiteERRORNotableConstraintofficial confidence
PRIMARY KEY constraint failed
Production Risk
Medium — INSERT fails; use UPSERT pattern (INSERT OR REPLACE) if appropriate.
What this means
SQLITE_CONSTRAINT_PRIMARYKEY (1555) is returned when an INSERT or UPDATE would create a duplicate primary key value.
Why it happens
- 1Inserting a row with an explicit primary key that already exists.
- 2Manually specifying an INTEGER PRIMARY KEY that conflicts with an existing rowid.
How to reproduce
INSERT or UPDATE where the PK column value already exists.
trigger — this will error
trigger — this will error
import sqlite3
conn = sqlite3.connect(':memory:')
conn.execute('CREATE TABLE t(id INTEGER PRIMARY KEY, val TEXT)')
conn.execute('INSERT INTO t VALUES(1, "a")')
try:
conn.execute('INSERT INTO t VALUES(1, "b")')
except sqlite3.IntegrityError as e:
print(e) # UNIQUE constraint failed: t.idexpected output
sqlite3.IntegrityError: UNIQUE constraint failed: t.id
Fix 1
Fix 2
Fix 3
Sources
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev