// SQLite error reference

SQLITE_CONSTRAINT_PRIMARYKEY: PRIMARY KEY constraint failed

An INSERT or UPDATE would duplicate a primary key value. SQLite's message still says UNIQUE constraint failed and names the key column.

Result code
SQLITE_CONSTRAINT_PRIMARYKEY
Name
1555
Category
Extended result codes
Error message
UNIQUE constraint failed: orders.id

What it means

SQLITE_CONSTRAINT_PRIMARYKEY (1555) is the extended code for a duplicate primary key. The message text is the same as for any unique violation, UNIQUE constraint failed: table.column, so the extended code is how a program tells the two apart.

For an INTEGER PRIMARY KEY column you can leave the value out (or pass NULL) and SQLite assigns the next rowid, which avoids most collisions.

Common causes

  • Inserting an explicit id that already exists.
  • Copying rows between databases with their ids, where the ranges overlap.
  • Generating ids in the application with a scheme that can repeat.

How to fix it

  1. Let SQLite assign INTEGER PRIMARY KEY values by leaving the column out.

    INSERT INTO orders (customer_id, total) VALUES (42, 19.99);
    SELECT last_insert_rowid();
  2. When merging data, upsert on the key or skip existing rows.

    INSERT INTO orders (id, customer_id, total)
    SELECT id, customer_id, total FROM other.orders WHERE true
    ON CONFLICT (id) DO NOTHING;

Related errors

More extended result codes errors

The code and message text are from the SQLite result and error codes documentation. Browse the full SQLite error list or search every database error.

// fewer errors, faster fixes

Work on SQLite in SQLly

A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for SQLite and the other databases you use.