StoicBuilder

HandbookXI the TabulariumDesigning a schema

XIthe Tabularium, Model the data

Designing a schema

1 min readUpdated 28 November

In plain words

Designing a schema means deciding which tables exist, what each row stands for, how tables link through keys, and which rules the database itself enforces, so bad data can't get in even when the code has bugs.

Where you see it

Harbor needs teams, members, plans, subscriptions and invoices. Each becomes a table with a primary key, each invoice points to its team with a foreign key, and constraints refuse an invoice with a negative amount or a second active subscription for the same team.

supabase/migrations/20261201090000_invoices.sqldatabase
create table invoices ( id uuid primary key default gen_random_uuid(), team_id uuid not null references teams(id) on delete restrict, amount_cents bigint not null check (amount_cents >= 0), currency char(3) not null, status text not null check (status in ('draft', 'open', 'paid', 'void')), issued_at timestamptz not null default now());
Money in whole cents, every invoice tied to a team, and statuses the database won't let anyone misspell.

What goes wrong

  1. Storing money as a floating-point number

    In floating point, 0.1 + 0.2 is 0.30000000000000004, and invoice totals drift by a cent. Store whole cents in an integer column, or use numeric, and keep the currency beside it.

    total float
  2. Rules that live only in the app code

    If only the TypeScript checks that a team has one active subscription, a second code path, a script or two simultaneous requests will break the rule. Put it in the database as a constraint or unique index.

    create unique index one_active_sub on subscriptions (team_id) where status = 'active';
  3. One wide table for everything

    A users table with plan_name, plan_price, team_name and invoice_1_total columns gets harder to change with every feature. Give each kind of thing its own table and link them with keys.

What to tell your agent

Copy this into Lovable, Bolt, Cursor or any agent
Before writing any code, propose the schema for this feature as SQL: tables, primary and foreign keys, not null and check constraints, and the indexes our queries will need. Store money as integer cents with a currency column, and explain what each constraint protects against.

Quick check

Sign in to earn +10 XP for it

A Harbor team must never have two active subscriptions. Where is the safest place to enforce that?

Got a prompt that touches designing a schema?

Paste it. You get a better prompt first, then the cards that explain it.

Check your own prompt

Free, no sign-up.