I found myself debugging why an ON DELETE CASCADE was not working, until realizing that foreign keys must be enabled for every database connection, with PRAGMA foreign_keys = <boolean>; (ref: https://www.sqlite.org/pragma.html#pragma_foreign_keys).
From a development point of view, I feel it is complex to manage when to enable/disable SQLite foreign keys. To be on the safe side, I would rather hook this statement with ON, for every connection. But then I wonder what is the actual penalty or reason for not having such setting persistent across DB connections.
So my questions are:
- Why didn't SQLite choose to have foreign keys enabled by default?
- Assuming 1. has good reasons, why didn't SQLite choose to have this setting persistent at database file or table level, instead of being per connection?
- Can you give an idea of how big a penalty it is to use foreign keys enabled for operations that do not require them?
If it matters, I'm using the C implementation of SQLite, version 3.22.0.