{
  "slug": "sqlite-foreign-key-check",
  "term": "SQLite foreign_key_check",
  "aliases": [
    "PRAGMA foreign_key_check"
  ],
  "definition": "SQLite foreign_key_check reports violations of foreign-key references declared in a database schema.",
  "note": "Validation and enforcement are separate operations. Enabling foreign_keys inside a transaction does not change enforcement; read it outside the transaction. [pragma]\n\nA 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. [rules] [experiment]\n\nA 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. [replace-data]\n\nA 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. [lifetime-rules] [lifetime-results]\n\nValidate 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. [lifetime-analysis]\n\nMATCH 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. [match-rules] [match-evidence] [match-replay]",
  "related": [
    "sqlite-integrity-check",
    "backup-verification",
    "sqlite-online-backup",
    "sqlite-replace",
    "sqlite-foreign-key-action"
  ],
  "citations": [
    {
      "id": "pragma",
      "title": "SQLite PRAGMA foreign_keys and foreign_key_check",
      "url": "https://www.sqlite.org/pragma.html#pragma_foreign_key_check",
      "accessed": "2026-10-17"
    },
    {
      "id": "rules",
      "title": "SQLite Foreign Key Support",
      "url": "https://www.sqlite.org/foreignkeys.html",
      "accessed": "2026-10-17"
    },
    {
      "id": "experiment",
      "title": "Hesela: executed SQLite validation-boundary cases",
      "url": "https://hesela.com/analyses/backup-verification-is-not-a-restore-test/#sqlite-validation-boundaries",
      "accessed": "2026-10-17"
    },
    {
      "id": "replace-data",
      "title": "Hesela: 24 matrix cases, four global-deferral controls and two orphan controls",
      "url": "https://hesela.dev/research/sqlite-replace-fk/results.json",
      "accessed": "2026-10-21"
    },
    {
      "id": "lifetime-rules",
      "title": "SQLite foreign-key schema commands: external UNIQUE-index exception",
      "url": "https://sqlite.org/foreignkeys.html#fk_schemacommands",
      "accessed": "2026-11-11"
    },
    {
      "id": "lifetime-results",
      "title": "Hesela: parent-key lifetime observations, sources and offline replay",
      "url": "https://hesela.dev/research/sqlite-parent-key-lifetime/results.json",
      "accessed": "2026-11-11"
    },
    {
      "id": "lifetime-analysis",
      "title": "Hesela: parent-key integrity depends on the index lifetime",
      "url": "https://hesela.com/analyses/sqlite-index-runtime-dependencies/#parent-key-lifetime",
      "accessed": "2026-11-11"
    },
    {
      "id": "lifetime-replay",
      "title": "Hesela: complete parent-key lifetime offline replay package",
      "url": "https://hesela.dev/research/sqlite-parent-key-lifetime/sqlite-parent-key-lifetime.zip",
      "accessed": "2026-11-11"
    },
    {
      "id": "match-rules",
      "title": "SQLite: composite keys and unsupported MATCH clauses",
      "url": "https://sqlite.org/foreignkeys.html#fk_unsupported",
      "accessed": "2026-11-19"
    },
    {
      "id": "match-evidence",
      "title": "Hesela: five-schema MATCH FULL comparison derived from native captures",
      "url": "https://hesela.dev/research/sqlite-match-full/summary.json",
      "accessed": "2026-11-19"
    },
    {
      "id": "match-replay",
      "title": "Hesela: complete MATCH FULL code, retained database images and tests",
      "url": "https://hesela.dev/research/sqlite-match-full/sqlite-match-full.zip",
      "accessed": "2026-11-19"
    }
  ],
  "status": "shipping",
  "advantages": [],
  "limitations": [
    "A checker execution error must not be converted to an empty violation list; successful completion is a separate acceptance condition."
  ],
  "supersedes": [],
  "supersededBy": [],
  "firstSeen": null,
  "lastSeen": null,
  "updated": "2026-11-19",
  "provenance": [
    "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."
  ]
}
