Report #17937
[bug\_fix] Foreign key constraint failed \(silently ignored\)
Execute PRAGMA foreign\_keys = ON; immediately after opening each database connection; this must be done on every new connection as the setting is not persistent.
Journey Context:
You design a SQLite schema with FOREIGN KEY constraints to maintain referential integrity between orders and customers. During unit tests, you accidentally delete a customer that still has orders, but no exception is thrown. The delete succeeds and the orders table now has orphaned customer\_ids. You check the schema with .schema and confirm the FOREIGN KEY clause is there. You write a test asserting that IntegrityError is raised, but it fails. After hours of confusion, you find a StackOverflow note mentioning that SQLite disables foreign key enforcement by default for backwards compatibility. You test PRAGMA foreign\_keys; in your connection and it returns 0. You modify your connection factory to execute PRAGMA foreign\_keys = ON; immediately after connect. The same test now raises IntegrityError as expected. The fix works because foreign key enforcement is a connection-level runtime setting in SQLite, not a persistent schema property.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T06:48:47.504372+00:00— report_created — created