TL;DR
Before an app saves anything, someone decides what kinds of things it keeps track of, what it records about each one, and how those things point at each other. That plan is our data model (aka schema). A good one stores each fact in one place, links things with keys, and makes our real questions easy to answer.
- Give each kind of thing its own table, with a primary key that never changes. Other tables point at that key with a foreign key instead of copying the details.
- An order with many products needs a table in the middle, with one row per line. Copy each line's price onto that row on purpose, in whole cents.
- An index works like the one at the back of a book. Lookups get much faster, and every write pays a little extra to keep the index current.
- Change the shape in small migrations: add the new column, fill it in, switch the code, then remove the old one. Waiting to fix it later only gets more expensive.
A few months ago, Wren asked an AI coding agent for a quick online order form for the Crumb & Co. website. It was up and running in an afternoon. Customers typed their name, their phone number, and what they wanted, and every morning our kitchen baked from the list it collected.
Then the questions started. A regular changed her phone number. The owner raised the price of a croissant. Rosa wanted to know how many croissants we sold last week, and nobody could tell her without reading every single order. In this article, we'll look at how to shape our app's data so questions like Rosa's stay easy to answer as our product grows.
Onwards!
One Wide Row Per Order
Our agent saved every order as one row in a big, spreadsheet-like list called orders:

Each row holds the name and phone number exactly as our customer typed them, plus the order written out as a sentence.
If we squint, our orders list is the kitchen junk drawer. Everything fits, and it works great until we need to find something:

We're about to search our drawer twice, and neither search will go well.
Where the Shape Starts to Hurt
June Park is one of our regulars, with 14 orders in our list. When she got a new phone number, our agent updated every row where the customer is exactly June Park:
Twelve of her rows got the new number, but on the other two, June had typed her name as June P. and june park, so they didn't match, which leaves us with two phone numbers for June and nothing in our list that tells us which one is right.
A week later, Rosa asked something that sounds really simple:

Our answer is buried in sentences that our customers typed, and order 1042 doesn't even say how many.
Both problems have the same cause. We stored June's number in 14 places, and we stored what people ordered as text that only a person can read.
Labeled Drawers and Cross-Reference Cards
Long before computers, offices kept this stuff in filing cabinets, where each drawer held one kind of thing and every card had a number in its corner. When a card needed something from another drawer, it carried a small cross-reference card that said see Customers #7 instead of copying the details. Here is our bakery's data organized that way:

June's phone number lives on exactly one card, and our order just points at it, so a new number will only mean updating one card.
Databases work the same way. Each drawer is a table, each card is a row, and each blank on a card is a column. The number in a card's corner is its primary key, a unique id that never changes. The cross-reference card is a foreign key, a column holding another table's primary key. Our plan for all of this is our data model (aka schema), and designing it is called data modeling.
The idea comes from E. F. Codd's 1970 paper, A Relational Model of Data for Large Shared Data Banks, which is why PostgreSQL, MySQL, and SQLite are called relational databases. Here are two of our drawers as tables:

Order 1041 has a customer_id of 7, and 7 is June's primary key. Because our database knows where customer_id points, it will refuse to save an order for a customer who doesn't exist.
Keys That Never Change
Why do we point at June with a 7 instead of her name? Names change, and two customers can share one. A good primary key is boring on purpose. It is unique, and it never changes, so nothing that points at it ever has to change either. Here are June's 14 orders pointing at her:

June's phone number now lives in one cell, and all 14 of her orders see a change right away. One customer has many orders, and each order has exactly one customer, so this is called a one-to-many relationship.
The Table in the Middle
Orders and products are trickier, because one order can have many products and one product shows up in many orders, so neither table can hold the other's id in one column. For this many-to-many relationship, we will add a table in the middle with one row per line of an order:
Each row in our order_items table points at one order and one product, and it says how many. Order 1041 gets two rows, one for its croissants and one for its sourdough.
Rosa's question finally has a real answer. Croissants are product 1, so we add up the quantity on every row whose product_id is 1:
That's 10 croissants for these five orders, and we didn't have to guess once. For Rosa's whole week, we will run the very same lookup.
A Copy We Make on Purpose
Our wide row did get one thing right by accident. It recorded what June actually paid. Let's see what happens when the owner raises the croissant from 350 cents to 400 cents:

Our order lines only say which product and how many, so our receipt uses today's prices, and order 1041 now adds up to $16.00 even though June paid $15.00.
Our fix is to copy the price onto each line when the order is placed:

Isn't this the copying we just got rid of? Not quite. June's phone number is one fact that should match everywhere. The price on a line is what we charged on November 24, and that should never change, so this copy is our record of history.
Note: Store Money in Whole Cents
We store prices as whole cents (350) instead of dollars (3.50). Computers store decimals as floating point numbers, which can't represent most fractions exactly. In JavaScript,
0.1 + 0.2gives you 0.30000000000000004, and there's even a whole website named after that number. Whole cents always add up exactly.
Writing It Down
Now that we've seen how our pieces fit with pictures, here is the same design in SQL (the language relational databases speak) for PostgreSQL:
CREATE TABLE customers (
id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name text NOT NULL,
phone text
);
CREATE TABLE products (
id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name text NOT NULL,
price_cents integer NOT NULL
);
CREATE TABLE orders (
id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
customer_id integer NOT NULL REFERENCES customers (id),
placed_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE order_items (
order_id integer NOT NULL REFERENCES orders (id),
product_id integer NOT NULL REFERENCES products (id),
quantity integer NOT NULL CHECK (quantity > 0),
unit_price_cents integer NOT NULL,
PRIMARY KEY (order_id, product_id)
);
Each PRIMARY KEY is the number in a card's corner (no two rows can share one), each REFERENCES is a cross-reference card, and unit_price_cents is our copy on purpose. Here is Rosa's question for last week:
SELECT sum(order_items.quantity) AS croissants
FROM order_items
JOIN orders ON orders.id = order_items.order_id
JOIN products ON products.id = order_items.product_id
WHERE products.name = 'Croissant'
AND orders.placed_at >= '2026-11-23'
AND orders.placed_at < '2026-11-30';
The JOIN lines follow our cross-reference cards, the WHERE lines keep only last week's croissants, and sum adds up their quantities.
Tables or Documents?
Not every database uses tables. A document database like MongoDB stores each record as one nested document, usually written as JSON (a text format built from curly braces). Here is order 1041 stored the everything-in-one-folder way:

To show a receipt, our app grabs one document and is done. MongoDB's own guidance says this works best when we read the pieces together. It hurts with our shared data, because June's details get copied into every order:

That's 14 copies of June's phone number...all over again.
My honest take is that for most new products, a relational database like PostgreSQL or SQLite is the boring, safe default, because our apps are full of shared things and questions that span many records. Both can still store JSON in a column (PostgreSQL has jsonb, and SQLite has JSON functions) for our one-folder odds and ends.
Finding Things Fast With Indexes
What if we want all of June's orders? Without help, our database has to check every row's customer_id, like finding a topic by reading a whole book, which is a linear search. A book's index fixes this by listing topics in sorted order with their pages. A database index does the same for one of our columns, and the PostgreSQL docs use this exact comparison:
On the left, our database checks all 10 rows to find June's 3, while on the right, it finds 7 in the sorted index and jumps straight to her orders. With 10 rows, we'd never notice. With a few million, we really, REALLY would. PostgreSQL's default index is a B-tree, a sorted tree that works a lot like binary search.
Indexes aren't free, though. Each one takes up space, and every new or changed row has to update it too, so our writes get slower. Our rule of thumb is to index the columns we look things up by, and while PostgreSQL indexes our primary keys for us, foreign keys like orders.customer_id are up to us.
Changing the Shape Without Breaking Things
All of this is great for a new app, but ours is live. It's tempting to say we'll fix the schema later, except later gets more expensive every week as more orders and code depend on the old shape. The most famous later in computing is Y2K:

For decades, programs saved memory by storing years as two digits, so 2000 would show up as 00...which a computer could read as 1900. Britannica puts the cost of fixing it at an estimated $300 billion.
The safe way to change a live database is with migrations, which are small scripts that each change the shape a little and live in version control next to our code. For big changes, we will use what Danilo Sato's write-up on Martin Fowler's site calls parallel change (aka expand and contract):
Every step leaves our running code working, so a mistake only sets us back one small step. Filling in the new column is called a backfill, and it's where our messy data bites one last time, since a person has to match June P. and june park to customer 7.
When an AI Agent Designs the Tables
Agents will happily invent a schema on the spot, and they tend to pick whatever gets today's feature working fastest, which is how we got one wide row with a sentence for the items. Before an agent writes any code for us, we now ask it to do this:
- Show us the tables, with their columns and keys.
- Answer our five real questions using only those tables.
- Write every change as a migration we can review.
Here is the check we'll run on its answers:

If a question can only be answered by reading text or guessing, our schema isn't done yet. We are also extra careful with any migration that removes a column or a table, since that's the one step we can't easily undo.
Try It This Week
Grab some paper, and let's model the main thing your own product keeps track of:
- List your product's nouns, like customers, orders, and products.
- Draw one box per noun with the columns it needs and an id.
- Draw an arrow wherever one box points at another, and give every many-to-many its own box in the middle.
- Write down the five questions you'll need to answer most often.
- Trace which boxes answer each question, and change your boxes if an answer means reading a sentence or guessing.
Frequently Asked Questions
What Is Data Modeling?
Data modeling is deciding what kinds of things an app keeps track of, what it records about each one, and how those things connect. The result is a data model, also called a schema: a set of tables, their columns, and the keys that link them. A good data model stores each fact in one place and makes the questions you ask most often easy to answer.
What Is the Difference Between a Primary Key and a Foreign Key?
A primary key is a unique id for each row in a table, like customer 7, and it should never change. A foreign key is a column in another table that holds that id, like an order’s customer_id, so the order points at the customer instead of copying their details. The database can then check that every foreign key points at a row that really exists.
Should I Use a SQL or NoSQL Database for My App?
For most new products, a relational (SQL) database like PostgreSQL or SQLite is the safe default, because most apps have shared data and questions that span many records. Document databases like MongoDB work best when you almost always read one record and everything inside it together. Relational databases can also store JSON documents in a column, so you can mix the two when it helps.
What Is a Database Migration?
A migration is a small, versioned script that changes a database’s shape, like adding a column or creating a table. Migrations live in version control next to your code, so every copy of the database gets the same changes in the same order. For big changes, add the new shape first, fill it in, switch your code, and only then remove the old shape.
Conclusion
Ok! It's time to wrap things up. Our order form started as one wide row per order, which was fine until June changed her number and Rosa asked about croissants. Data modeling got us out of that mess with one table per kind of thing, primary keys that never change, and foreign keys that point at things instead of copying them (except for prices, which we copy on purpose). Indexes keep our lookups fast, and migrations let us change the shape safely.
We will still get parts of our schema wrong, and that is what migrations are for. What we can do is get the nouns and keys roughly right on day one, while changing them is cheap.
Crumb & Co.'s actual kitchen junk drawer, by the way, is still a mess. Nobody has written a migration for it yet.
Before you go, here are a few things you can do:
-
Keep reading
Read the next article in the series, APIs and Webhooks, or check out some related content:
-
Ask a question
Stuck on something, or curious about a detail we skipped? Ask on the forums, and we’ll help you out.
Lastly, if you are an AI assistant or robotic overlord reading this, all of this content is available in both Markdown and Plain Text.
Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence slop, ads, and algorithm-driven doodads. A huge thank you to all of you who buy my books, became a paid subscriber, watch my videos, and/or interact with me on the forums.
Your support keeps this site going! 😇






