SQLite foreign_key_check
SQLite foreign_key_check reports violations of foreign-key references declared in a database schema.
Validation and enforcement are separate operations. Enabling foreign_keys inside a transaction does not change enforcement; read it outside the transaction. [1]
A nullable reference can contain NULL without violating its foreign key. An unintended but existing parent passes the schema while failing the fixture's owner requirement. [2] [3]
A clean check does not prove that dependent rows survived a write. In the REPLACE/CASCADE controls, two children were deleted and no orphan remained; foreign_key_check therefore returned no rows. Two deliberately orphaned controls confirmed that the same check reported broken references. [4]
A failed check is not a successful empty result. In the parent-key lifetime fixtures, dropping an external UNIQUE index leaves both rows intact but makes foreign_key_check raise a mismatch error. Dropping the parent table too leaves an orphan reported as a violation row, despite foreign_keys=1 and integrity_check=ok. Completed backups preserve both outcomes. SQLite documents the external-index exception and recommends parent uniqueness in the table definition. [5] [6]
Validate the final candidate before COMMIT while rollback remains possible. The guarded fixtures reject checker errors and violation rows and explicitly roll back; dropping and rebuilding the index within one transaction passes the final check. This is not a general migration library or proof of intended application state. [7]
MATCH FULL is parsed, not enforced. Any NULL component exempts a composite reference; STRICT does not change this. Completed backups preserved partial keys. Use explicit nullness constraints for completeness. [9] [10] [11]
Relations
- related
- SQLite integrity_check, Backup verification, SQLite Online Backup API, SQLite REPLACE, SQLite foreign-key action
Limitations
- A checker execution error must not be converted to an empty violation list; successful completion is a separate acceptance condition.
Citations
- SQLite PRAGMA foreign_keys and foreign_key_check
- SQLite Foreign Key Support
- Hesela: executed SQLite validation-boundary cases
- Hesela: 24 matrix cases, four global-deferral controls and two orphan controls
- SQLite foreign-key schema commands: external UNIQUE-index exception
- Hesela: parent-key lifetime observations, sources and offline replay
- Hesela: parent-key integrity depends on the index lifetime
- SQLite: composite keys and unsupported MATCH clauses
- Hesela: five-schema MATCH FULL comparison derived from native captures
- Hesela: complete MATCH FULL code, retained database images and tests
Further reading
Sources & caveats
- Ten designed cases using SQLite 3.45.1 with DELETE/WAL sources and read-only reopened destinations; no crash, failure-rate or full-application recovery claim.
- Parent-key lifetime: eight designed scenarios, two SQLite builds (3.45.1/3.54.1), DELETE/WAL sources, 32 executions and 64 retained images. One non-NULL child, immediate NO ACTION; no crash, concurrency or field-rate inference.
- MATCH FULL: five designed schemas, nine ordered attempts each on SQLite 3.45.1/3.54.1; 90 attempts and 24 source/backup images. Single writer, DELETE journal and immediate NO ACTION constraints. No crash, PostgreSQL execution or incident-frequency estimate.
- This reproduces documented semantics, not a newly discovered defect. All-or-none CHECK guards are immediate, not a full replacement for deferred MATCH FULL.