Report #7136
[bug\_fix] Foreign key constraints not enforced despite FOREIGN KEY clause in schema \(SQLite\)
Execute \`PRAGMA foreign\_keys = ON;\` on every database connection immediately after opening it. The root cause is that SQLite parses and recognizes foreign key declarations by default, but enforces them only when this pragma is explicitly enabled to ON; the default is OFF for backward compatibility with older SQLite versions that did not support foreign keys.
Journey Context:
A developer creates a SQLite database for a mobile app with \`parent\` and \`child\` tables. They define \`child\(parent\_id\) REFERENCES parent\(id\) ON DELETE CASCADE\`. They test inserting a child row with a non-existent parent\_id; it succeeds without error. They delete a parent row; the child row remains as an orphan. Confused, they check the schema using \`.schema\` and see the FK clause is there. Thinking the database is corrupted, they recreate it. Same issue. They search online and find a StackOverflow answer mentioning the \`foreign\_keys\` pragma. They run \`PRAGMA foreign\_keys;\` in the CLI and get \`0\`. They add \`db.execute\('PRAGMA foreign\_keys = ON'\)\` to their app startup code immediately after creating the connection. Now deleting a parent correctly cascades to delete children, and inserting a child with a bad parent\_id raises \`FOREIGN KEY constraint failed\`.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T01:50:43.365164+00:00— report_created — created