|
Databases

How to structure data for a marketplace app

A marketplace has three parties in every order, and most first schemas only hold one. Sellers, variants, price snapshots and payouts, in the order that survives growth.

How to structure data for a marketplace app

A founder showed me his marketplace schema last year. Products had a seller_name column. Orders had a product_name column. Payouts were a spreadsheet he updated by hand every Friday. He had 340 sellers and no idea which ones were owed money.

The app worked. That is the frustrating part. It took orders and showed them to buyers. It just could not answer the questions a marketplace lives on: who sells what, who owes who, and what happens when a seller changes a price.

Marketplaces are harder than shops because there are three parties in every transaction. The schema has to hold all three.

The four tables that carry a marketplace

Start with sellers, products, orders and order_items. Buyers can be a users table, but do not overbuild that early. Many marketplaces run for a year with guest checkout and no buyer accounts at all.

  • sellers: id, business_name, phone, status, commission_rate, created_at.
  • products: id, seller_id, name, price_kobo, stock, active, created_at.
  • orders: id, buyer_name, buyer_phone, status, subtotal_kobo, delivery_fee_kobo, total_kobo, created_at.
  • order_items: id, order_id, product_id, seller_id, quantity, unit_price_kobo, commission_kobo.

Notice seller_id appears on order_items, not just on products. That is deliberate, and it is the detail most people miss.

If order_items only stores product_id, then the price of an old order changes every time the seller edits the product. Your revenue report for March will not match what you were paid in March.

So copy the price at the moment of purchase into unit_price_kobo. The product row can change forever. The order keeps the truth of what the buyer agreed to. Same reason you copy the seller id: the product might move to another seller, but this order belongs to the seller who fulfilled it.

This is called a snapshot, and it applies to names too. If you display the product name on an invoice, store it. Otherwise a seller renaming "Gold Chain" to "Gold Chain (sold out)" quietly rewrites your receipts.

Products and variants

A single products table breaks the moment a seller has sizes. Now you need a variants table.

product_variants: id, product_id, name, price_kobo, stock, sku.

The product holds the shared description and images. The variant holds the thing that is sold: small, medium, large, or 128GB versus 256GB. Order items should point to the variant, not the product, because that is what the buyer chose.

Do not build variants on day one if every seller sells one-size items. But design the order_items table so adding a variant_id later is a small change, not a rewrite. Leaving room is cheaper than being clever early.

Commission and payouts

A marketplace takes a cut. Decide where that number lives. I put commission_rate on the seller, copy it into the order item as commission_kobo at purchase time, and never recalculate history when the rate changes.

Then payouts become a query, not a spreadsheet. Money owed to a seller is the sum of commission adjusted item totals for delivered orders that have no payout record yet. That means you need a payouts table with id, seller_id, amount_kobo, period_start, period_end, status, paid_at. And a payout_id on the items, or a join table, so you can prove which orders a payment covered.

Without that link, two people will disagree about whether a seller was paid, and neither will have evidence.

Status is a story, not a flag

Beginners use one status on the order and hope it covers everything. It does not, because a marketplace order can be partly shipped. Three items from three sellers do not move together.

Put fulfilment status on the order item: pending, confirmed, shipped, delivered, cancelled. Put payment status on the order: unpaid, paid, refunded. Two separate fields, because they answer two separate questions.

Now "which sellers have unshipped items from paid orders" is a simple query. With one status column it is impossible.

What to avoid

Do not store the seller name on products. Join it. Do not store totals you can calculate unless the calculation can change later, and then store it with a note about when it was computed. Do not let sellers delete products that appear in orders. Mark them inactive.

And do not put money in a float. Kobo as integers, every time. I have seen a marketplace lose 40,000 naira to rounding over eight months because totals were floats and someone rounded at the wrong step.

Start with the order table

Write order_items first. If you can describe one row, you understand the marketplace: this order, this product, this seller, this quantity, this price, this commission. Everything else is a lookup around it.

Build those five tables in SQLite this week and insert one order with two items from two different sellers. Then write the query that shows each seller what they are owed. If that query works, the hard part is done.

For the basics behind these tables, read how to design a database for a small business app. If you are still choosing an engine, SQL versus NoSQL explains why a marketplace should almost always be relational. The software development course takes the same schema further into payouts and reporting.

Dave Partner

Dave teaches digital skills at Braintem and has helped thousands of Nigerians move from watching tutorials to charging for real work. He writes about the parts nobody puts in the course trailer.