Report #56279
[bug\_fix] Foreign key constraints not enforced \(SQLite silent data integrity failure\)
Execute PRAGMA foreign\_keys = ON immediately after opening every database connection. Verify with PRAGMA foreign\_keys check. Root cause is SQLite disables foreign key enforcement by default for backward compatibility with SQLite version 3.6.18 \(2009\) and earlier.
Journey Context:
A React Native app uses SQLite to cache relational data with foreign key constraints defined in CREATE TABLE statements. During testing, the developer notices that deleting a parent 'project' row leaves orphaned 'task' rows with invalid project\_ids, breaking the UI. Checking the schema confirms FOREIGN KEY \(project\_id\) REFERENCES projects\(id\) ON DELETE CASCADE is defined. The developer runs PRAGMA foreign\_key\_check and finds no violations, yet the orphaned data exists. After researching, they discover PRAGMA foreign\_keys returns 0 \(off\). They realize the SQLite driver opens connections with foreign keys disabled by default. They modify the connection initialization code to execute db.exec\('PRAGMA foreign\_keys = ON'\) immediately after opening, and add a check to throw if PRAGMA foreign\_keys returns 0. Deleting parent rows now properly cascades, and attempting to insert invalid foreign keys immediately throws, preserving data integrity.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T00:57:26.743325+00:00— report_created — created