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
- Insert parents first
- Delete children first or use ON DELETE CASCADE
- Use PRAGMA foreign_key_check to find violations
Detailed fix by platform
SQL
PRAGMA foreign_key_check;
How to diagnose
- Rows — Which parent/child pair?
- 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.
Was this page helpful?
Report a correction or suggest an improvement
Last updated 2 Oct 2026