Report #65608
[bug\_fix] Foreign key constraint violations allowed silently
Execute PRAGMA foreign\_keys = ON; immediately after opening every SQLite connection and before beginning any transaction; this must be done on a per-connection basis as the setting is not stored in the database file and defaults to OFF for backwards compatibility.
Journey Context:
A developer designs a SQLite schema with referential integrity: CREATE TABLE departments\(id INTEGER PRIMARY KEY\); CREATE TABLE employees\(id INTEGER PRIMARY KEY, dept\_id INTEGER REFERENCES departments\(id\)\). They assume the database will prevent orphaned rows. Their application opens a connection and runs INSERT INTO employees VALUES \(1, 999\); where 999 does not exist in departments. The insert succeeds with no error. Later, queries with JOINs miss data or reports show inconsistencies. The developer checks .schema and sees the FOREIGN KEY clause is there. They test in sqlite3 CLI and the same thing happens. They consult the SQLite documentation and discover that foreign key enforcement is disabled by default for backwards compatibility with older SQLite versions. The fix requires executing PRAGMA foreign\_keys = ON; for every connection. They modify their application code to run this pragma immediately after sqlite3\_open or sqlite3.connect\(\). After the change, the same INSERT now raises "FOREIGN KEY constraint failed". They must also ensure that if they use connection pooling, the pragma is set on every checkout, as it is a connection-local setting.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T16:36:17.437961+00:00— report_created — created