StoicBuilder

HandbookII the ForumRelations between tables

IIthe Forum, The shape of an app

Relations between tables

1 min readUpdated 28 November

In plain words

A relation links tables by storing one row's id inside another, so an order can point to its restaurant and each item can point back to its order.

Where you see it

In Supabase's Table Editor, QuickBite's orders table has a restaurant_id column holding numbers like 7. That column is a foreign key: 7 is the id of a row in the restaurants table.

supabase/migrations/20261201090000_create_order_items.sqldatabase
create table order_items ( id bigint generated always as identity primary key, order_id bigint not null references orders (id) on delete cascade, menu_item_id bigint not null references menu_items (id), quantity integer not null check (quantity > 0));
Each references line is a relation. on delete cascade removes an order's items when the order goes.

What goes wrong

  1. Copying data instead of linking it

    Storing the restaurant's name on every order means a rename leaves 4,000 orders with the old one. Store restaurant_id and look the name up.

  2. No foreign key, so orphans pile up

    Without references, you can delete a member and leave their posts pointing at nobody. The feed then crashes trying to read author.name.

  3. A cascade nobody expected

    on delete cascade in the wrong place means deleting one restaurant silently removes every order ever placed there, including the ones your accountant needs.

What to tell your agent

Copy this into Lovable, Bolt, Cursor or any agent
List the tables in this app and, for each one, which columns point to which other tables and what happens to linked rows when one is deleted. Flag anywhere data is copied instead of linked.

Quick check

Sign in to earn +10 XP for it

A QuickBite restaurant changes its name, but the orders page still shows the old one. Why?

Got a prompt that touches relations between tables?

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

Check your own prompt

Free, no sign-up.