SQLite FK failed 🗃️ SQL

sqlite3.IntegrityError: FOREIGN KEY constraint failed

An insert/update/delete violated a foreign key in SQLite (with PRAGMA foreign_keys=ON).

Seen on: SQL

Meaning

SQLite doesn’t say which key failed. Check parent rows exist and deletes don’t leave orphans. Foreign keys are off unless enabled per connection.

Common causes

  • Parent row missing
  • Deleting a parent with children
  • Insert order wrong

⚡ Quick fix

  1. Insert parents first
  2. Delete children first or use ON DELETE CASCADE
  3. Use PRAGMA foreign_key_check to find violations

Detailed fix by platform

SQL

  1. PRAGMA foreign_key_check;

How to diagnose

  1. Rows — Which parent/child pair?
  2. Pragma — foreign_keys enabled?

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.