pgref.dev/sqlite/errors/SQLITE_CORRUPT_SEQUENCE
SQLITE_CORRUPT_SEQUENCEERRORTier 3 — Handle with care⚠️ MEDIUM confidence

sqlite_sequence table is corrupt

Category: CorruptionVersions: 3.32.0+

🔴 Production Risk Error

High — AUTOINCREMENT inserts will fail.

What this means

SQLITE_CORRUPT_SEQUENCE (523) is returned when the sqlite_sequence table (which tracks AUTOINCREMENT counters) contains invalid or inconsistent data.

Why it happens

  1. 1sqlite_sequence table was manually edited or corrupted.
  2. 2Database file corruption affecting the sqlite_sequence table.

How to reproduce

INSERT into a table with AUTOINCREMENT when sqlite_sequence is corrupt.

trigger — this will ERROR
-- Manually corrupt sqlite_sequence (do not do in production):
UPDATE sqlite_sequence SET seq = -1 WHERE name = 'orders';
-- Next INSERT into orders → SQLITE_CORRUPT_SEQUENCE
sqlite3.DatabaseError: database disk image is malformed

Fix 1

Why this works

Fix the corrupted row in sqlite_sequence manually: UPDATE sqlite_sequence SET seq = (SELECT MAX(id) FROM orders) WHERE name = 'orders';

Fix 2

Why this works

Run PRAGMA integrity_check; and restore from backup if widespread corruption.

What not to do

Why it's wrong:

Version notes

Sources

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

🔧 Source ref: sqlite3.h — SQLITE_CORRUPT_SEQUENCE = 523

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 →