SQLiteSQLite SQLITE_CONSTRAINT

SQLite: UNIQUE Constraint Failed

SQLite raises a UNIQUE constraint failed error when an INSERT or UPDATE attempts to write a duplicate value into a column or combination of columns with a UNIQUE or PRIMARY KEY constraint.

Example Query

INSERT INTO users (id, email) VALUES (1, 'a@example.com');
-- Fails if id=1 or email='a@example.com' already exists

Common Causes

  1. 1Inserting a row with a primary key that already exists
  2. 2Duplicate values in a UNIQUE column
  3. 3Re-running a seed script without clearing the table

How to Fix It

Use INSERT OR IGNORE to skip duplicates, or INSERT OR REPLACE to overwrite them. Check for existence before inserting. Use autoincrement primary keys to avoid manual ID management.

Need a reliable database?

Try Supabase — free PostgreSQL with a generous free tier. No credit card required.

Get started free →

Got a query causing this error?

Paste it into SQLbuddy and get an instant plain-English explanation with optimization tips.

Analyze My Query →