Report #4694
[bug\_fix] SQLite foreign keys not enforced by default
Execute PRAGMA foreign\_keys = ON immediately after opening every database connection, before any other SQL is executed on that connection. Root cause: For backward compatibility with SQLite 3.5.9 and earlier, foreign key constraints are parsed but not enforced unless this pragma is explicitly enabled on a per-connection basis.
Journey Context:
A developer builds a desktop inventory app using SQLite. They carefully design the schema with FOREIGN KEY constraints to ensure referential integrity between 'products' and 'categories'. During development, they write a test that deletes a category that still has products. The test expects a foreign key violation, but the deletion succeeds silently, leaving orphan product rows. The developer checks the schema using .schema in the CLI and sees the FK constraints are defined correctly. They suspect a bug in the SQLite library. After searching StackOverflow, they find a mention of the foreign\_keys pragma. They connect to the DB in the CLI and run PRAGMA foreign\_keys; it returns 0 \(off\). The 'aha' moment comes from reading the SQLite foreign key documentation: they realize FK enforcement is off by default. They modify their connection initialization code to execute PRAGMA foreign\_keys = ON immediately after opening each connection. Re-running the test now correctly throws 'FOREIGN KEY constraint failed'. Data integrity is restored.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T19:55:40.961885+00:00— report_created — created