Foreign Key Discipline

Use FKs; trade-offs.

Overview

Foreign-key discipline uses database FKs to enforce referential integrity at the engine level rather than relying on ORM checks or application logic. ORM-level enforcement misses anything that bypasses the ORM (raw SQL, batch scripts, restored backups); FKs catch all of it. The discipline is in adopting FKs by default, always indexing the FK column, choosing ON DELETE behavior deliberately rather than letting the ORM pick, and managing FK validation carefully during migrations.

The approach

The practical approach is FK by default for any reference between tables, always index the FK column at creation, choose ON DELETE deliberately based on the parent-child semantics, use NOT VALID then VALIDATE on Postgres for adding FKs to large tables without table locks, and document the per-FK rationale so the relationship semantics survive team turnover.

Why this compounds

FK discipline compounds across the lifetime of the schema. Each enforced relationship catches application bugs the next reviewer would not have spotted; each indexed FK keeps dependent-query performance bounded; the team builds a vocabulary for relationship semantics that pays off on every new table. Without the discipline, referential integrity bugs surface as customer-reported data anomalies months after introduction.

FK discipline is a database discipline that pays off across years. Nova AI Ops integrates with database telemetry, surfaces relationship patterns, and supports the team’s schema design discipline.