Report #101016
[bug\_fix] Foreign-key constraints are silently ignored; inserts succeed but leave orphaned rows; ON DELETE CASCADE does not run.
Execute PRAGMA foreign\_keys=ON on every database connection immediately after opening it. Foreign-key enforcement is off by default for backwards compatibility and is a connection-local setting, so it must be set in the connection setup hook of the ORM/driver; it cannot be stored in the database file. Also remember the pragma is a no-op inside a transaction, so set it before BEGIN.
Journey Context:
You create tables with FOREIGN KEY references in SQLite and run tests. Everything looks correct, but in production you discover child rows with no matching parent. You check the schema and the FK syntax is right. You test in the sqlite3 CLI and it enforces FKs, but your Python app does not. After reading the SQLite docs you learn that foreign-key enforcement is disabled by default and must be enabled per connection. Your ORM connection factory was not running PRAGMA foreign\_keys=ON, so every web request opened a connection with no referential integrity. You add the pragma to the connect\_args/connection setup hook, and now violations raise FOREIGN KEY constraint failed. The cascade deletes also start working. The key insight is the pragma is connection-local, not a database property, and is ignored if issued inside a transaction.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-06T04:50:40.813660+00:00— report_created — created