sqlite-foreign-key-check · shipping · also PRAGMA foreign_key_check

SQLite foreign_key_check

SQLite foreign_key_check reports violations of foreign-key references declared in a database schema.

Related Observatory article: Read on hesela.com

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

  1. SQLite PRAGMA foreign_keys and foreign_key_check (accessed 2026-10-17)
  2. SQLite Foreign Key Support (accessed 2026-10-17)
  3. Hesela: executed SQLite validation-boundary cases (accessed 2026-10-17)
  4. Hesela: 24 matrix cases, four global-deferral controls and two orphan controls (accessed 2026-10-21)
  5. SQLite foreign-key schema commands: external UNIQUE-index exception (accessed 2026-11-11)
  6. Hesela: parent-key lifetime observations, sources and offline replay (accessed 2026-11-11)
  7. Hesela: parent-key integrity depends on the index lifetime (accessed 2026-11-11)
  8. SQLite: composite keys and unsupported MATCH clauses (accessed 2026-11-19)
  9. Hesela: five-schema MATCH FULL comparison derived from native captures (accessed 2026-11-19)
  10. Hesela: complete MATCH FULL code, retained database images and tests (accessed 2026-11-19)

Further reading

  1. Hesela: complete parent-key lifetime offline replay package (accessed 2026-11-11)
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.

JSON: /data/terms/sqlite-foreign-key-check.json

Published by Hesela team · Updated

Report a correction · Editorial standards · Reuse guidance

Cite this page
Hesela team. SQLite foreign_key_check. Hesela. 2026-11-19. https://hesela.dev/terms/sqlite-foreign-key-check/