Report #3786
[bug\_fix] FOREIGN KEY constraint failed \(SQLite\) with inconsistent enforcement
Explicitly execute \`PRAGMA foreign\_keys = ON;\` immediately after opening every database connection; do not assume the default.
Journey Context:
Developers test a Flask app locally with SQLite; foreign keys appear to work \(enforced\). However, in CI tests using the sqlite3 CLI tool to load fixtures, inserts with invalid foreign keys succeed silently, causing later application code to fail with 'FOREIGN KEY constraint failed' when the app \(which has foreign\_keys=ON\) tries to modify related rows. The rabbit-hole reveals that SQLite's \`foreign\_keys\` pragma defaults to OFF for backwards compatibility, and many drivers \(like Python's sqlite3\) inherit this default. The fix requires explicitly setting the pragma on every connection, ensuring consistent referential integrity enforcement across CLI, application, and test environments.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T18:13:03.950466+00:00— report_created — created