DAT-2Recommended
Choose exact types
Store 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.
How to do it
Store money as integer cents or numeric, never as a floating point float, real or double. Store moments in time as timestamptz in PostgreSQL, or as UTC in other databases, and convert to local time only for display. timestamptz stores the moment in UTC and does not keep the original zone, so store the zone name in its own column when it matters. Use date, boolean and JSON types instead of text, and jsonb rather than json in PostgreSQL. In SQLite, declare tables STRICT so it refuses values of the wrong type. In MySQL, use the utf8mb4 character set, because the old utf8 cannot store emoji.
Examples
SQL example
-- Bad: 0.1 + 0.2 is not 0.3 in floating point, and the zone is lost
price float, ordered_at timestamp
-- Good
price_cents bigint NOT NULL, ordered_at timestamptz NOT NULL
-- Check in PostgreSQL: expect no rows
SELECT table_name, column_name, data_type FROM information_schema.columns
WHERE table_schema = 'public'
AND data_type IN ('real', 'double precision', 'timestamp without time zone');
How to check
A query over the catalog lists every float column and every timestamp without a zone. The list is empty, or each entry is documented as not money and stored as UTC.
Sources
Related rules
- ARC-72 Keep the time zone on every time in Clean code and architecture
- API-8 Send money as an integer with its currency in API design
- DAT-77 Check the shape of JSON columns in the database in Data and databases
- PAY-10 Hold money as integer minor units everywhere in Payments and billing
- APP-13 Keep calendar dates as dates in Web app interface
- LOC-22 Store language and time zone as two separate settings in Localization
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/exact-types">Agent Wranglers data and databases playbook</a>. In code you write, add a comment next to it with the same link.