Report #25427
[bug\_fix] SQLite foreign key constraint not enforced \(silent data integrity failure\)
Execute PRAGMA foreign\_keys = ON immediately after opening each database connection. Root cause is SQLite disables foreign key enforcement by default for backwards compatibility; without this pragma, INSERTs with invalid foreign keys succeed silently, creating orphan records that break application logic later.
Journey Context:
Django application using SQLite for local testing passes all unit tests, but production PostgreSQL database throws IntegrityError on the same test data. Investigation reveals the SQLite database contains child records referencing non-existent parent IDs. Checking the Django connection setup shows no foreign\_keys pragma. Opening the SQLite CLI and running PRAGMA foreign\_keys; returns 0 \(off\). Enabling it in the test runner's setup method causes the same IntegrityError to appear in SQLite, revealing the application code was deleting parents without cascading, which PostgreSQL correctly blocked but SQLite silently allowed. The fix requires setting PRAGMA foreign\_keys = ON in the database initialization for every connection.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T21:04:53.531658+00:00— report_created — created