Report #17270
[bug\_fix] Foreign key constraint ignored \(silent failure\)
Execute \`PRAGMA foreign\_keys = ON;\` on every database connection immediately after opening it. Unlike PostgreSQL, SQLite disables foreign key enforcement by default for backwards compatibility with versions prior to 3.6.19; constraints are parsed and stored but not enforced unless this pragma is enabled.
Journey Context:
An iOS developer using GRDB \(Swift SQLite wrapper\) implements a parent-child relationship with \`REFERENCES\` clauses in the schema. Unit tests pass because they use in-memory databases where the pragma might accidentally be on, or they don't test invalid inserts. In production, users report data inconsistencies: child records exist with \`parent\_id\` values pointing to deleted parents. The developer debugs by opening the production DB in \`sqlite3\` CLI and running \`PRAGMA foreign\_keys;\` which returns \`0\`. They insert a test row with an invalid FK and confirm no error is raised. The realization hits: SQLite requires explicit opt-in for FK enforcement. The fix involves adding \`db.execute\("PRAGMA foreign\_keys = ON"\)\` immediately after every \`sqlite3\_open\` call in the app startup sequence. After the fix, testing invalid inserts immediately throws \`SQLITE\_CONSTRAINT\_FOREIGNKEY\`, and the data integrity issues are resolved.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T04:53:42.517394+00:00— report_created — created