Report #84146
[bug\_fix] SQLite foreign key constraint not enforced despite REFERENCES clause
Execute PRAGMA foreign\_keys = ON; on every database connection immediately after opening. Unlike other databases, SQLite defaults to foreign\_keys=OFF for backwards compatibility, meaning FOREIGN KEY clauses are parsed but ignored unless explicitly enabled.
Journey Context:
A new developer created a schema with FOREIGN KEY\(user\_id\) REFERENCES users\(id\) and assumed referential integrity was protected. During testing, they accidentally deleted a user and noticed the orphaned rows remained in the child table without any error. Checking the table schema confirmed the FK was defined. After hours of confusion, they discovered a footnote in the SQLite documentation stating that foreign key enforcement is disabled by default and must be enabled per connection. The fix was adding a single PRAGMA execution in the application's connection factory, which immediately caused the expected foreign key constraint violations to raise errors, preventing data corruption.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T23:49:43.210584+00:00— report_created — created