Skip to content

Foreign keys only on master data; none on transactional tables

Accepted. Supersedes ADR 0007, whose plan to remove every foreign key was not carried out.

2026-11-10

ADR 0007 ruled that invariants live in the domain and planned to drop all 16 original constraints. The plan was never executed, and newer tables (functions, push, portal and field identity, ingestion) added more foreign keys. By the time of this ruling 40 existed. Two things were clear from the audit. Foreign keys on high-volume, pruned or partitioned tables are a real cost: they do not survive partitioning, and they add locking and cleanup coupling to tables written per request or per message. Foreign keys between low-volume configuration tables that people edit are cheap, useful and easy to reason about.

The owner’s ruling: foreign keys may be kept on some tables, but never on ones that ever need to be partitioned, and they should be avoided in future. Master data is the best candidate; transactional data should ideally never have them.

  • Master and configuration data may keep their foreign keys. These are low-volume tables that people edit: applications, roles, principals, clients, OAuth clients, connections, subscriptions, pools, function definitions, ingestion definitions, field-worker identities and their grants, push configuration. Both ends of a key must be master data.
  • Transactional data carries none. A table is transactional when its rows are written per request or per message (a sign-in, a poll, a push, a delivery), when retention or an expiry sweep prunes it, or when it is, or could one day need to be, partitioned. Events, dispatch jobs and attempts, traces, logs, login attempts, sessions, tokens, challenges, leases, runtime reports, push ids, audit and outbox tables are all transactional.
  • No new foreign keys by default. A new table references another by a plain, indexed column. A key is allowed only when both tables are master or configuration data and the key earns its place, for example a junction owned by its parent and cleaned up by cascade.
  • Columns and indexes stay when a constraint goes. A dropped ON DELETE CASCADE becomes code: the parent’s delete path removes the children in the same transaction, or the harmless orphans are pruned by retention.
  • The list is enforced. An integration test lists every foreign key the schema may hold and fails on any other, so adding one is a deliberate change.

Migration 114 dropped the eight constraints on transactional tables (email PINs, trusted devices, reset-approval requests, ingestion leases, route states and secret fetches, field sign-in codes and push ingest ids). The master-data keys stay, including the two cross-aggregate ones that ADR 0007 worried about. One ingestion table (ing_state) is kept pending an owner decision.

Good:

  • Master-data deletes keep the database’s help; the high-volume tables are free of cross-table constraints and can be partitioned or moved.
  • The rule is short enough to apply in review and is checked by a test.

Bad:

  • Two integrity models coexist, and a reader must know which tables are master data.
  • Without the key, a write that races a parent’s delete can leave an orphan. For the dropped tables the orphans are either swept by retention or name a principal that no longer exists, so nothing can use them.
  • Remove every foreign key (ADR 0007). Consistent, but it discards cheap integrity on configuration data and was never completed.
  • Keep them everywhere. Rejected for partitioned and pruned tables.