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
  1. 1Inserting a row with an explicit primary key that already exists.
  2. 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.id

expected output

sqlite3.IntegrityError: UNIQUE constraint failed: t.id

Fix 1

Fix 2

Fix 3

Sources
Official documentation ↗

sqlite3.h — SQLITE_CONSTRAINT_PRIMARYKEY = 1555

SQLite UPSERT

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All SQLite errors