SQLITE_CONSTRAINT_UNIQUE
SQLiteERRORNotableConstraintofficial confidence

UNIQUE constraint failed

Production Risk

Medium — UPSERT patterns handle this gracefully.

What this means

SQLITE_CONSTRAINT_UNIQUE (2067) is returned when an INSERT or UPDATE would create a duplicate value in a column (or combination of columns) declared UNIQUE.

Why it happens
  1. 1Inserting a row whose UNIQUE column value already exists in the table.
  2. 2Bulk import containing duplicate values in a unique-constrained column.
How to reproduce

INSERT or UPDATE violating a UNIQUE constraint.

trigger — this will error
trigger — this will error
import sqlite3
conn = sqlite3.connect(':memory:')
conn.execute('CREATE TABLE users(email TEXT UNIQUE)')
conn.execute("INSERT INTO users VALUES('a@b.com')")
try:
    conn.execute("INSERT INTO users VALUES('a@b.com')")
except sqlite3.IntegrityError as e:
    print(e)  # UNIQUE constraint failed: users.email

expected output

sqlite3.IntegrityError: UNIQUE constraint failed: users.email

Fix 1

Fix 2

Fix 3

Fix 4

What not to do

Sources
Official documentation ↗

sqlite3.h — SQLITE_CONSTRAINT_UNIQUE = 2067

SQLite UPSERT

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

← All SQLite errors