Sub topic 6
Modeling your data
This chapter assumes you know how web apps use pages, forms and accounts, and that you have scoped one core user flow. Your data model is the set of tables and fields that flow needs. Get it right now, because every screen and workflow you build later depends on it.
Start from the core flow
Read your core flow sentence and underline the nouns. For a freelancer task tracker, the flow is "a signed-in user creates a task with a title, client name and due date, sees only their own tasks, and marks a task done." The nouns are user and task. Each noun that you store becomes a table, and each detail about it becomes a field.
Keep only what the flow needs. Teams, files and payments are out of scope, so they get no tables yet.
Tables, fields and ownership
A table holds one kind of thing. A field holds one fact about it. Each table needs:
- A primary key, a unique id for each row.
- Typed fields, such as text, date or number, so bad values are rejected.
- An owner field that links the row to a user, if users must see only their own data.
A relationship links two tables. Here, one user owns many tasks. You store that link as a foreign key, which is a field that holds the id of a row in another table.
Constrain the values
Free text invites inconsistency. If status can be "Done", "done" or "finished", filters and counts break. Limit it to a fixed set of values. Most no-code tools offer a select or enum field type. On a Postgres backend you can use a check constraint.
Enforce ownership in the database
Hiding other users' tasks in the interface is not enough. Anyone can call the backend directly. On a Postgres backend such as Supabase, row-level security makes the database apply the rule itself.
create table tasks (
id uuid primary key default gen_random_uuid(),
owner_id uuid not null references auth.users(id),
title text not null,
client_name text,
due_date date,
status text not null default 'todo' check (status in ('todo', 'doing', 'done')),
created_at timestamptz not null default now()
);
alter table tasks enable row level security;
create policy "owners read own tasks" on tasks
for select using (auth.uid() = owner_id);
create policy "owners insert own tasks" on tasks
for insert with check (auth.uid() = owner_id);
create policy "owners update own tasks" on tasks
for update using (auth.uid() = owner_id) with check (auth.uid() = owner_id);using filters which existing rows a user can touch. with check validates the new values being written. The update policy needs both so a user cannot reassign a task to someone else.
Common errors
- Storing a list of values in one text field, such as "design, writing". Use a related table instead.
- Copying a client's name into every task when clients will need their own data. Decide early whether a client is a field or a table.
- Leaving out the owner field, then trying to add access rules later.
- Using text for dates, which breaks sorting and filtering.
Exercise
A user can update a task and set owner_id to another user's id, even though the select policy works. Which policy clause is missing or wrong, and why?
Show answer
The update policy lacks with check (auth.uid() = owner_id). using only controls which existing rows the user may update. with check validates the new row values, so without it the user can write a different owner_id.