Rzemiosło · The Craft
What is The CraftLevelsGet startedChapters Search Download PLEN
Level 3 · Codex ~2 min read

In one sentence: Design data so it doesn’t duplicate and changes easily: lookups, slug instead of ID, history instead of overwriting.

This chapter in plain terms

This chapter is about the shape of your data — how to arrange information in the database so a change is cheap and nothing gets lost. A bad layout takes revenge on every layer of the app.

Each piece of information in one place. A country, a category or a product type you keep in a separate lookup table, not as free text typed in each time. Otherwise you get five spellings of the same thing (“USA”, “U.S.A.”, “United States”) and can’t sensibly filter or count them.

Identify by a stable key (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 by a number. Numbers (IDs) can change — for instance when you 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 → duplicates. If you tie a user’s review to a changing number, after a database rebuild it lands on the wrong product. A slug (a readable name in the address, e.g. mountain-bike) is stable, so it’s safe.

Instead of overwriting — retire. Changing a price? You don’t delete the old one, you mark it “inactive” and add a new one. The history stays, and you get an audit trail (“what changed and when”) for free.

Example: a display name (“brand + model + edition”) you compute on the fly, not store in a separate column — otherwise after a brand change that column lies until someone fixes it by hand.

Read more: database normalization · a slug in a URL · foreign key (relations between tables).

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

  1. Controlled vocabularies live in lookup tables (countries, types, tags) plus a CHECK where values are fixed — never free text in a column.
  2. Many-to-many goes through a junction table, never primary_x + secondary_x columns.
  3. 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).
  4. 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.
  5. Computed values are computed, not stored (e.g. a display name from its parts) — stored, they drift.
  6. 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.
  7. 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).
  8. The schema is documented: an ERD plus the allowed values of every controlled field, in docs/.
  9. 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.
Chapter deep-dive 11Why these rules, examples and stories from projects — what the core deliberately leaves out.

The canonical doctrine is written in English.