Report #45784
[bug\_fix] SQLite silent foreign key constraint violations \(orphan rows\)
Execute "PRAGMA foreign\_keys = ON;" immediately after opening every database connection. Root cause: SQLite disables foreign key enforcement by default \(foreign\_keys pragma defaults to OFF\) for backwards compatibility with SQLite 3.6.19 and earlier; constraints are parsed but not enforced, allowing orphan rows and dangling references.
Journey Context:
A Django application uses SQLite for its test suite. All tests pass, but when the same application is tested against PostgreSQL in CI, multiple tests fail with IntegrityError: insert or update on table "orders" violates foreign key constraint. Investigation reveals the SQLite test database contains Order rows referencing Customer IDs that were deleted during test teardown. Checking the SQLite documentation uncovers that foreign\_keys is OFF by default. The Django SQLite backend actually executes PRAGMA foreign\_keys = ON when it creates the connection, but the test setup uses a raw sqlite3 connection for fixture loading that does not enable the pragma. Adding the pragma to the raw connection setup causes the SQLite tests to fail immediately with the same integrity errors as PostgreSQL, revealing that the application's cascade delete logic was incomplete and previously passing tests were false positives due to SQLite's permissive defaults.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T07:19:31.960638+00:00— report_created — created