pgref.dev/sqlite/errors/SQLITE_CONSTRAINT_UNIQUE
SQLITE_CONSTRAINT_UNIQUEERRORTier 2 — Caution⚠️ MEDIUM confidence

UNIQUE constraint failed

Category: ConstraintVersions: 3.8.0+

🔴 Production Risk Error

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
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
sqlite3.IntegrityError: UNIQUE constraint failed: users.email

Fix 1

Why this works

Use INSERT OR IGNORE to skip duplicates silently.

Fix 2

Why this works

Use INSERT OR REPLACE to overwrite the existing row.

Fix 3

Why this works

Use the UPSERT clause: INSERT ... ON CONFLICT(email) DO UPDATE SET ...

Fix 4

Why this works

Check for existence before inserting: SELECT 1 FROM users WHERE email=?

What not to do

Why it's wrong:

Sources

📚 Official docs: https://www.sqlite.org/rescode.html#constraint_unique

🔧 Source ref: sqlite3.h — SQLITE_CONSTRAINT_UNIQUE = 2067

📖 Further reading: SQLite UPSERT

Confidence assessment

⚠️ MEDIUM confidence

Stable.

See also

⚙️ This error reference was generated with AI assistance and reviewed for accuracy. Examples are provided to illustrate common scenarios and may not cover every case. Always test fixes in a development environment before applying to production. Spotted an error? Suggest a correction →