Designing an orders schema you will not regret
Money, history and state: the three things an order table gets wrong, and how to avoid it.
Order tables age badly. Not because they are complicated on day one, but because the business keeps changing around them and the table was written as if it would not.
Three decisions cause most of the pain.
1. Copy the price onto the order
create table order_items (
order_id bigint not null references orders(id),
product_id bigint not null references products(id),
product_name text not null, -- copied
unit_price_cents int not null, -- copied
qty int not null check (qty > 0),
primary key (order_id, product_id)
);product_name and unit_price_cents look like duplication. They are not. An order is
a record of something that happened. The product's price today has nothing to do with
what someone paid in March, and the product's name might have changed twice since.
If you join to products to render an old invoice, your invoices change over time. That
is a real problem in an audit and an unpleasant one to discover.
2. Store money as integers
numeric is correct. float is not, and should never be used for money. Integer minor units
(cents, poisha) is what I default to: it is exact, it is fast, and every client
language handles it without a decimal library.
Store the currency alongside it. A column called total with no currency is a bug
waiting for your first international customer.
total_cents int not null,
currency char(3) not null default 'BDT'3. Model status as a real thing, and keep its history
A single status column tells you where an order is now and nothing about how it got
there. The first time support asks "when was this cancelled and by whom", you cannot
answer.
create table order_status_history (
id bigserial primary key,
order_id bigint not null references orders(id),
status text not null,
actor_id bigint,
reason text,
created_at timestamptz not null default now()
);
create index on order_status_history (order_id, created_at);Keep the current status on orders as well, so the common query stays a single-table
read. That is deliberate denormalisation, and it is worth it. The alternative is a
correlated subquery on every list screen.
Constraints do work for you
qty int not null check (qty > 0),
total_cents int not null check (total_cents >= 0),
status text not null check (status in ('pending','paid','shipped','delivered','cancelled'))Every one of these is a class of bug that can no longer reach your data. Application validation is good; a database constraint is the thing that is still true after someone runs a fix-up script at 2am.
Foreign keys likewise. "We handle it in the application" works until two applications write to the same database.
Soft delete, carefully
Orders should never be hard deleted. But a plain deleted_at column means every query
in the codebase must remember where deleted_at is null, and one day one of them will
not.
Options, in the order I would try them:
- Do not delete. Cancel. Cancellation is a status, and it is what the business actually means.
- If you must, put a view over the table with the filter baked in and query the view.
What I would check before shipping a schema
- Can I reproduce an invoice exactly as it looked a year ago?
- Can I answer "who changed this and when"?
- Is every money column an integer with a currency next to it?
- Does a bad value have somewhere to hide, or does a constraint stop it?
- Is there an index for the one query that runs on every screen?
Five questions. They catch most of what would otherwise take a migration under pressure to fix later.