Schema design
Let the database guard its own data. Use constraints, exact types and stable keys, so no bug in any app can store a row that makes no sense.
- Recommended 8
Recommended
Enforce rules with constraints
DAT-1Put every rule the data must always follow into the database as a constraint. App checks alone fail when a second app, a script or a bug writes directly.
Choose exact types
DAT-2Store each value in the type that means it. Money is an exact number, time is a time with a zone, and true or false is a boolean.
Give every row a stable key
DAT-3Give every table a primary key that never changes. Never use a value people can edit, such as an email or a name, as the key.
Name things the same way everywhere
DAT-4Name tables, columns and constraints by one convention, so people and agents can guess a name without looking it up.
Keep one fact in one place
DAT-5Store each fact once, and derive the rest. Copies of a fact drift apart sooner or later.
Plan how rows are removed
DAT-6Decide for each table what deleting means, and make the foreign keys say it.
Keep the history of records that settle disputes
DAT-76For tables whose old values settle disputes, such as prices, plans, roles and delivery addresses, keep every earlier version of each row. Record when it changed and who changed it, in the same transaction.
Check the shape of JSON columns in the database
DAT-77Give every JSON column a check constraint for the keys and types the code relies on. Move any value you filter, join or sum on into its own typed column.
Read this with
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/data/schema-design">Agent Wranglers data and databases playbook</a>. In code you write, add a comment next to it with the same link.