Report #9291
[bug\_fix] Foreign key constraints silently ignored or ForeignKeyConstraintError
Execute PRAGMA foreign\_keys = ON; immediately after opening each database connection. This must be done on every connection; it is not a persistent database setting.
Journey Context:
A developer creates a SQLite database with foreign key constraints \(e.g., REFERENCES users\(id\) ON DELETE CASCADE\). During testing, they notice that deleting a user with existing posts does not cascade or prevent deletion, and inserting posts with invalid user\_ids succeeds without error. They verify the schema using .schema and see the constraints defined correctly. Confused, they search and find the SQLite Foreign Key Support documentation which explicitly states that foreign key enforcement is OFF by default for backwards compatibility with older SQLite versions. They modify their application code to execute cursor.execute\('PRAGMA foreign\_keys = ON'\) immediately after creating each connection \(e.g., in a connection factory or ORM setup\). Upon restart, the same test cases now properly throw ForeignKeyConstraintError when inserting orphans, and CASCADE/RESTRICT behaviors work as expected.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T07:46:54.214460+00:00— report_created — created