Database and Data Modeling

Decide who owns each record, link records with rules the database enforces, and test with made-up data that one customer cannot see another's.

Content date: October 15, 2026.

Plain English

A data model lists the things your app stores (students, parents, bookings), how they connect, and the rules that keep them sensible. A primary key identifies one record. A foreign key points from one record to another, such as a booking to its student. A constraint makes the database reject bad data. An index speeds up finding records. It does not decide who may read them.

Use this when

Do this before you store shared or private information. A public page with no sign-in can start without a database.

If you are just starting

Model one workflow with few fields. Name the owner or tenant (the customer organization whose data it is) of every private record. Say which links are required and what deleting a parent does. Use made-up data from two tenants. Put the permission check in every query. A tenant_id column alone does not keep tenants apart.

When your project grows

Enforce rules in the database where you can. Use parameterized queries, which send user input separately from the SQL so it cannot rewrite the query. Add indexes after you have seen the real queries. Test exports and background jobs across ownership lines too. SQLite ignores foreign keys unless you turn them on for each connection. On Postgres (including Supabase), row-level security (RLS) policies make the database filter rows by who is asking. Supabase advises enabling RLS on every table in an exposed schema, and its secret key bypasses RLS, so keep that key on your server.

Worked example

Chess Club and Garden Club each have a document called welcome. A key of club plus document ID lets both exist, and a matching owner link stops anyone attaching Chess Club's document to a member of only Garden Club. Reading still needs the caller's verified membership.

Practice

Model two tenants and challenge the boundaries

  1. Draw the records and required links for one workflow.
  2. Create made-up records for two tenants, using the same document ID (welcome) in both.
  3. Add required-field and link constraints in a throwaway database.
  4. Test a valid insert, a document with an owner from the other tenant, a duplicate ID inside one tenant, and a wrong-tenant read.
  5. Write down what happens when a member is deleted, and whether each guarantee came from a constraint, query, or access check.

Deliverable: A small schema, an ownership map, and your valid, invalid, and denied test results.

Common mistakes to watch for

  • A booking lookup filters by booking ID but not by center, so a parent who edits the URL reads another family's booking.
  • Only app code checks that a booking points to a real student, and one buggy import creates bookings for students who do not exist.
  • Deleting a tutor wipes every booking tied to them, or leaves bookings pointing at nobody.
  • Indexes pile up on every column with no evidence from real queries.
  • The signup form collects a child's birth date that nothing uses.

Prompting

A weak prompt

Make me a database with all the tables a booking app usually has.

A better prompt

Model this one workflow: [describe it, for example parents book tutoring sessions]. Use made-up data from two tenants. List owners, keys, required fields, links, and delete behavior, and justify any index from the queries. Build it in a throwaway database and test valid data, broken links, duplicate IDs, and a wrong-tenant request. Say which guarantees come from constraints and which need permission checks.

A schema built around one real workflow, plus a wrong-tenant test, exposes ownership mistakes before a customer sees someone else's data.

Check before you move on

  • Every stored field is one the workflow needs.
  • Every private record has a named owner or tenant.
  • The database enforces required links between records.
  • Every query on private data includes the tenant or owner check.
  • You chose what happens to child records on delete.
  • Each index traces back to a real query.

You are done when

  • Valid records save, and records with broken links are rejected in your test database.
  • A wrong-tenant request is denied, and you can name the layer that denied it.

Questions to think about

Every booking row has a tenant_id. A parent from Center A edits the URL to a booking ID from Center B. What decides whether they see it?

The server's query and permission check. A stored tenant_id is just data, so every read must filter by the verified tenant or be covered by a database policy.

A foreign key stops a booking from pointing at a student at another center. Does that stop other centers from reading it?

No. The constraint keeps data valid. Permission checks or row-level policies control who reads.

A teammate wants an index on every column "just in case." What does that cost?

Each index takes storage and slows every insert and update. Add one when a real, slow query needs it.

Official references