In the first post I planned the whole pantry tracker: what it does, the order I'm building it in, and why I picked each tool. Step one of the actual build is the database, because everything else depends on how the data is stored.
I expected this to take an evening. It took longer, mostly because my first ideas kept turning into better ones once I asked "what happens when...". This post walks through those decisions, including the ones I changed my mind on. Those turned out to be the most useful part.
The starting point
Supabase is a hosted Postgres database, the classic kind with tables, columns and links between tables. On top of that it adds a login system, and that saved me a whole table. Supabase keeps its users in its own table, so I don't build one. My tables just store the user's id to say who something belongs to.
My first thought was that I needed one table for pantry items. That turned into five, and the rest of this post is how.
What counts as one item?
My requirements said an item has a name, a quantity, an expiry date and a category. So the obvious design was one row like "Yogurt, quantity 5, expires Friday".
Then I thought about how I actually buy yogurt. Five yogurts are often bought on different days, so they have different dates. And they don't all end the same way: I might eat three and throw two away. A single row with "quantity 5" can't say any of that.
So I dropped the quantity. Every physical thing gets its own row, with its own expiry date and its own ending. Five yogurts are five rows. Even eggs can be stored one by one.
That sounds tedious to enter, but it doesn't have to be. The app can have a "×5" field that creates five rows in one go, and the list can group them back together on screen ("Yogurt ×5, expires Friday"). The database stays simple and exact, and the user never has to add things one at a time.
Products and pantry items
One row per yogurt raised a new problem. Five rows that all say "Greek yogurt, 500 g, barcode 3017..." repeat the same details five times.
My first idea was a link table between users and items, with extra fields for things like the expiry date. It felt like a separate idea at first. It wasn't. It was the same idea, seen from another side.
The trick is to split "item" into two things:
- A product is the kind of thing: "Greek yogurt 500 g". It's stored once.
- A pantry item is one actual tub in my fridge: "that yogurt, expires Friday, still there". It points to its product.
So five yogurts are one product and five small pantry item rows, each holding only what's different about it. Pantry items are exactly the link table I had in mind: they connect a user to a product and carry the extra details.
There's one difference from a textbook link table. Normally each pair (this user, this product) appears only once. Here the same pair appears many times, once per tub, so every pantry item gets its own id.
Barcodes, and who owns a product
When you scan a barcode, the app asks Open Food Facts, a free product database, for the name. That raised a question I hadn't expected: should products be shared between all users, or should each user have their own?
Shared sounds efficient: scan a yogurt once and everyone benefits. But the data from Open Food Facts is written by volunteers, so names can be odd, too long or in the wrong language. Users need to be able to fix them. And if products are shared, one person renaming "Activia Natural 4x125g" to "Yogurt" would change it for everyone.
So products belong to the user. To still avoid asking Open Food Facts about the same barcode again and again, there's a separate shared table that simply remembers what Open Food Facts said for each barcode. Nobody edits it; it's just a memory.
Here I went down one more path first. My idea was that a user's product could store only their changes ("overrides") and fall back to the shared data for everything else. It works, but every time the app reads a product it has to combine two tables. Worse, if the shared data ever refreshed, a product the user never touched could suddenly get a new name.
The simpler version won: the scanned data just pre-fills the form. When the user saves, the product row gets its own copy of the name. Nothing is ever overwritten. Next time they scan the same barcode, the app finds their product straight away, with their name on it.
At a glance
Solid arrows are links the database checks. The dashed one is the pre-fill: the app copies a name from the barcode memory when a product is first saved, and the two are separate from then on.
Tracking waste
One of my later goals is waste stats: how much food I actually throw away. That needs planning now, even if the stats come much later.
My first idea was a simple "used" yes/no flag. The problem: "used" can't tell eaten from thrown away, which is the whole point of waste stats. So each pantry item has a status instead, one of three: in the pantry, used, or thrown away. It also records when the status changed.
Then there's the half-empty bottle of cola. Thrown away, but only half of it. I added two things for that:
- Amount wasted, a number from 1 to 100 percent. It only applies to thrown-away items, and the app fills in 100 unless the user changes it.
- A note, free text like "half the bottle" or "opened Monday". It's only for the user to read and never used in any calculation.
I first wanted the amount to go from 0 to 100, just in case someone enters 0. But "thrown away, 0% wasted" really means "used up". Allowing it would give two different ways to record the same thing, and the stats would quietly get messier. So 0 isn't allowed: the app's slider starts at 1, and the database refuses 0 if anything gets past the app.
The main list in the app only shows items still in the pantry. Used and thrown-away items move to a history view that I'll rarely open.
Small choices that matter
Ids. Every row needs an id. The classic option is counting up: 1, 2, 3. The catch is that /items/42 tells anyone there's probably an item 43, and roughly how many items exist. So I went for UUIDs: long random ids like 9f1c2e4a-... that can't be guessed. (A UUID is random, not a secret code, so the backend still has to check that an item belongs to the person asking.)
I'll admit I didn't love how long they look. I wanted something short and nice, like a 6-character code. So I worked out what that would cost. With 6 letters and digits, there are about 57 billion possible ids, which sounds like plenty. But random ids clash sooner than you'd think: with 100,000 rows there's roughly a 9% chance two of them end up the same. I'd need code to catch that and try again, plus my own id generator. And Supabase's user ids are UUIDs anyway, so I'd have two styles of id side by side. UUIDs need none of that extra work, so I went back to them.
Dates. An expiry date has no time of day, so the column is a plain date. I briefly thought about storing it as text like "2026-11-20". But text would happily accept "2026-02-31", and questions like "what expires in the next 3 days?" get harder. The text format does still show up in one place: when dates travel between the app and the backend, they're sent as "2026-11-20", and the backend turns them into real dates.
For "created at" and "updated at" I use full timestamps with time zone, so they mean the same moment wherever the server or the user is. The backend sets "updated at" itself on every change, rather than the database doing it automatically, so all the logic stays in one place.
Optional fields. A field is only optional when real life gives a reason to leave it empty. Salt and honey don't expire, so the expiry date is optional. But it should be a deliberate choice, not something skipped by accident. In the app, the date picker is shown by default, with a "No expiry date" checkbox next to it. The backend also insists that the date is always sent, either as a date or as an explicit "none", so a bug can't silently save items without dates. Items without a date go to the bottom of the list.
Other optional fields have the same kind of reason: a barcode (homemade soup has none), the note, the amount wasted (only for thrown-away items) and when the status changed (empty while the item is still in the pantry). Everything else is required.
Fixed lists. Categories come from a fixed list so the dropdown can't end up with "Dairy", "dairy" and "diary". The list lives in its own small table, so the database itself rejects anything not on it. The three statuses work the same way. Their keys are readable words like dairy or discarded instead of random ids, because the code needs to refer to them by name.
Names. Table and column names are lowercase with underscores (pantry_item, expiry_date), which is what Postgres is happiest with. Tables are singular: product, not products.
Safety nets in the database
The app and the backend will check what users enter. But bugs happen, so the database gets a few rules of its own. If something slips past the app, the database refuses to save it.
- Amount wasted is 1 to 100, and it's filled in exactly when an item is thrown away, never otherwise.
- "Status changed" is empty while an item is in the pantry, and filled in once it's used or thrown away.
- Product names can't be empty, and they're at most 100 characters.
- No duplicate barcodes for the same user. Different users can have the same barcode, because each has their own product. But one user shouldn't end up with two products for the same barcode, which could happen if they type a product by hand and later scan it. This rule makes sure "find my product with this barcode" never returns two answers.
- A pantry item can only use the user's own product. This one is easy to miss. A normal link between tables only checks that the product exists, not whose it is. A bug could let my yogurt point to someone else's product. Linking on two columns together (the product's id and its owner) makes the database check ownership too.
One more thing helps the main screen stay quick. The database can keep a sorted shortcut to the rows, called an index, a bit like the index at the back of a book. Mine covers only items still in the pantry, sorted by owner and expiry date. Over time most rows will be used or thrown away, so leaving those out keeps the shortcut small.
The full schema
This is the same design as the overview above, with every column and its type. Arrows point from the column that holds a link to the table it links to. The link from a pantry item to its product uses two columns together, which is how the database makes sure the product belongs to the same user.
Keeping the data private
This one surprised me. Supabase automatically creates a web API for your tables, and the apps carry a public key that can call it. That's handy for some projects, but in mine only the Go backend is supposed to touch the data. Anyone could pull the key out of the app and read the tables directly, skipping all my backend's checks.
There are three ways to close that door:
- Turn on row-level security with no rules. This is Postgres's built-in permission system for rows. Switched on with no rules at all, it means "nobody gets anything" through the automatic API. My Go backend connects as the owner of the tables, which these rules don't apply to, so it keeps working.
- Switch the automatic API off in the Supabase project settings. Logging in still works, because that's a separate service.
- Move the tables somewhere the API can't see, a separate section of the database that isn't exposed.
I did the first two. Switching the API off says clearly what the setup is: only Go talks to the database. Row-level security is the backup if that setting ever gets switched back on. The third option is more useful when you want to hide some tables but keep the API for others, which isn't my case.
Writing changes down: migrations
You could build all of this by clicking around in the Supabase dashboard. The problem is that nothing records what you did, so you can't repeat it or see how the database changed over time.
The usual answer is migrations: numbered SQL files, each describing one change, plus a tool that remembers which files have already been run. Supabase has its own tool for this. I picked goose, a Go tool, instead. My plan says the Go backend owns the database, so the files describing the database should live next to the Go code that uses it.
My design ended up as three files:
- The two fixed lists (categories and statuses), with their values filled in.
- The shared barcode memory and the users' products.
- Pantry items, with their rules and the shortcut for the main screen.
Each file has an "up" part that makes the change and a "down" part that undoes it. I run them by hand from the terminal, so I always know when the database changed.
To connect, goose needs a connection string: one line holding the database's address, user name and password. In Supabase it's behind the Connect button at the top of the project page. I use the "session pooler" version, because it works on any network. It's as sensitive as a password, so it goes in an environment variable and never into Git.
One gotcha I almost hit: I wanted to run the first files by pasting them into Supabase's SQL editor. That editor doesn't understand goose's markers, so it would run the "down" part too and delete the tables it had just created. And goose wouldn't know the files had already run. Running everything through goose from the start avoids both problems.
What I put off on purpose
A few things came up that I decided not to solve yet. It's a learning project, and an unfinished decision is better than a rushed one.
- Deleting data. What happens to someone's items when they delete their account, or to a product that pantry items still point to? The usual options are deleting everything linked to it, blocking the delete, or marking rows as deleted without removing them. For now the database simply blocks deletes that would leave broken links.
- Shared households. Right now everything belongs to one user. Sharing a pantry with family or flatmates would mean items belong to a household instead. Moving to that later is a one-time change, and it'll be easier if the backend checks "does this belong to you?" in one single place.
- Partial amounts. "Half the flour left" could become an extra field later without changing anything else.
- Reminders. The daily expiry reminders will need some per-user settings, like time zone and how many days of warning.
What's next
The database is ready. Next up is the Go backend: connecting to the database, checking who's logged in, and the first real endpoints for adding and listing pantry items.
For more, you can follow me on LinkedIn, GitHub, or Instagram.





