Skip to content

No foreign keys: invariants live in the domain

Superseded by ADR 0052 (2026-11-10), which keeps foreign keys on master data and forbids them on transactional tables. The text below is the original ruling.

Originally: accepted as a rule for new schema. Partly implemented: the removal plan for the existing constraints has not been carried out (see Consequences).

2026-08-27

The schema was created with 16 foreign keys. Fifteen of them exist to delete or mutate child rows when a parent is deleted (ON DELETE CASCADE or SET NULL) rather than to validate anything. The review that led to this ruling made four points.

  1. They do not partition. The high-volume messaging tables are range-partitioned by created_at, in development as well as production, so that constraint shapes that do not survive partitioning fail early. A foreign key onto, or from, a partitioned table is either unsupported or constrains the partitioning strategy.
  2. They sit at the wrong boundary. Roles and principals’ role assignments are different aggregates. A constraint between them encodes cross-aggregate integrity in the one place that cannot travel with the aggregate to another store or to a sharded layout.
  3. They hide business rules. ON DELETE RESTRICT is the domain rule “a role in use cannot be deleted”, written where no test covers it, no reviewer reads it and no error message explains it. Two cascades were worse: deleting a service principal silently deleted the OAuth client that referenced it, and deleting a principal silently nulled an application’s service-account link. Neither was expressed in Go, tested or audited.
  4. They rely on implicit cascading. The caller learns of a constraint violation rather than a reason.

The immediate trigger was role assignment: iam_principal_roles.role_name is a plain string column and no write path checked that the role exists, so a typo resolved silently to no permissions at token-mint time. A foreign key would have caught that, but the ruling was that the domain, not the schema, should own the check.

Invariants live in the domain. Use cases validate references; repositories delete a parent’s children explicitly in the same transaction; cross-aggregate effects of a delete are decided, evented and tested rather than inherited from a constraint. The columns and indexes stay; only the constraints go.

The plan to remove the existing 16 was phased:

  1. make each parent’s delete remove its children, with a test that asserts no orphans;
  2. decide the two cross-aggregate cases deliberately (refuse, or act with an audit record);
  3. drop the constraints in one migration;
  4. add a scheduled reconciliation check that counts orphans and exposes a metric, because removing a constraint removes a guarantee.

A parallel change introduces typed identifiers and constructors that resolve a role name against the role table, so an unvalidated assignment cannot be built.

Good

  • The delete path states what it deletes, and tests can prove it.
  • Partitioned and high-volume tables are not constrained by cross-table references, and the schema can move to another store with less rework.
  • Business rules appear in code, with error messages that explain them.

Bad

  • Removing a constraint removes a safety net. Integrity moves from the write path to tests and an asynchronous check, and a missed delete leaves orphans.
  • Not finished. Reading the migrations at the time of writing, the 16 original constraints are still present, and the plan’s migration to drop them does not exist. The newer function, push, portal-identity, field-identity and ingestion tables also declare foreign keys to their own parents (mostly ON DELETE CASCADE), although later migrations note that high-volume ingestion tables carry none. The ruling is therefore best read as the direction for cross-aggregate references and for anything that must partition, with intra-aggregate cascades still used in places.
  • A rollback of the drop migration fails if orphans have accumulated, which is intended.
  • Keep foreign keys and add domain checks too. Duplicates the rule, and keeps the partitioning and boundary problems.
  • Foreign keys only for non-partitioned reference data. Workable, but it keeps two integrity models and the hidden cascade behaviour.
  • Deferred or NOT VALID constraints as an audit. Considered as a way to keep the check without the lock cost; superseded by the reconciliation check.