11 — Data model and normalization
Commandments V and VII at the root: the schema is the contract of the whole system. Normalize first; denormalize deliberately, with a named source of truth.
Rules
- Controlled vocabularies live in lookup tables (countries, types, tags) plus a
CHECKwhere values are fixed — never free text in a column. - Many-to-many goes through a junction table, never
primary_x+secondary_xcolumns. - The stable identifier is the Slug — A readable, short part of a page address that describes its content in words instead of a mysterious number. Better for humans and SEO; a stable slug doesn’t break links when things change. more in the glossary →, not the numeric id. Ids drift after Merge — Combining the changes from one branch into another. A merge brings in every commit from the source branch — other people’s too — so first you check what exactly comes in. more in the glossary →; user data is mapped
slug → new id, a missing slug is skipped and logged (→ 19). - Active row instead of overwrite where history matters: expire the old row (
expired_at), insert the new one; a partial unique Index (database) — A lookup in the database that makes searching instant instead of scanning everything in order. The first move for slow queries — like an index at the back of a book instead of reading 400 pages. more in the glossary → (WHERE expired_at IS NULL) enforces one active row per key. Audit for free. - Computed values are computed, not stored (e.g. a display name from its parts) — stored, they drift.
- A Cache — A temporarily remembered result, so the same thing isn’t computed again on every request. Speeds the app up, but can be a trap: a stale cache shows old data. more in the glossary → or Snapshot — A saved state of data from a specific moment — e.g. a database copy or a file of computed statistics. It speeds things up and keeps them safe, but is stale from the moment it’s taken — you need to know where fresh data comes from. more in the glossary → names its source of truth and falls back to it when stale; never two sources for one fact.
- Migration (database) — A controlled change to the database layout — adding a column, a table or moving data — step by step. Like a renovation to plan: rebuilding data in a set order so nothing “collapses”. more in the glossary → are forward-only and additive, scripts gate on a column/table existing, and every data
operation ends with
foreign_key_check(→ 04). - The schema is documented: an ERD plus the allowed values of every controlled field, in
docs/. - Declared privacy is enforced by the model: sensitive processing runs as a separate Idempotency — A property of an operation you can run many times with the same result — no duplication. Key for scripts and events: a re-run doesn’t break data. Like an “ON” switch. more in the glossary → Pipeline — A chain of processing steps run in order, e.g. fetch → clean → enrich → check. When each step runs on its own and repeats safely, a failure halfway doesn’t mean starting from zero. more in the glossary → on anonymized data, with a test that re-identification is impossible (→ 09).
Anti-patterns
- 🚫 Free text where a lookup belongs (“USA”, “U.S.A.”, “United States”).
- 🚫 One fact stored in two places without synchronization.
- 🚫 User data mapped by a mutable id.
- 🚫 A stored computed value that drifts.
- 🚫 A destructive migration under running old code.