Off by default — turn them on
SQLite supports foreign keys, but they are off by default for backward compatibility. You must enable them per connection:
PRAGMA foreign_keys = ON;This is the single most common SQLite footgun. The schema accepts FK declarations and the diagrams look right — but inserts that violate them succeed silently because the engine isn't checking. Pippa, like every SQLite-using framework, runs this PRAGMA on every new connection.
Warning:
PRAGMA foreign_keys is per-connection, not per-database. Set it as the first statement on every connection. Connection pools must do this in their connect_args / on-connect callback.Once enabled, FKs:
- Reject inserts whose FK doesn't match any row in the parent table.
- Reject deletes from the parent that would orphan child rows (unless you specify
ON DELETE CASCADEor similar). - Optionally cascade updates and deletes — covered in lesson w08.