Report #5376
[bug\_fix] SQLite foreign key constraints ignored \(no error on delete\)
Execute PRAGMA foreign\_keys = ON; immediately after opening every database connection; this is a per-connection setting and is off by default. Root cause: SQLite disables foreign key enforcement by default for backwards compatibility with older versions that lacked the feature; the DDL is parsed but not enforced unless the pragma is enabled.
Journey Context:
You define a schema with CREATE TABLE orders \(user\_id INTEGER REFERENCES users\(id\) ON DELETE CASCADE\);. During testing, you delete a user and expect the orders to disappear. They remain. You verify the schema is correct—pragma foreign\_key\_list\(orders\) shows the constraint. You delete again—still orphan rows, no error. You check the SQLite docs and find the "Enabling Foreign Key Support" section stating foreign keys are off by default. You open your app's database initialization code and realize you never executed the pragma. You add a connection setup hook that runs cursor.execute\("PRAGMA foreign\_keys = ON"\); immediately after connect\(\). You re-run the test: deleting the user now cascades and deletes orders. You verify in production logs that the pragma is set on every new connection. The constraint now enforces referential integrity because the per-connection flag enables the enforcement engine.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T21:10:56.577287+00:00— report_created — created