Short answer
A required column was left empty, so Postgres refused the whole row. In apps built with Lovable, Bolt or Cursor it is almost always the owner column: a server function or webhook inserts with the service role key and forgets user_id, the browser sends only the form fields to a table whose insert policy does not check the owner, or a sign-up trigger leaves a profile column empty. Let the database fill in the owner, send it from the verified user on the server, and fix the trigger. Never fix it by dropping NOT NULL on an owner column.
Ask an AI about this page:ChatGPTClaude
On this page
A column marked not null got no value, so Postgres refused the whole row with error code 23502. Nothing was saved. The message names the column and the table, so you know exactly what is missing. In AI-built apps the missing value is almost always the owner of the row, user_id, and the real question is why nobody filled it in.
Find your cause in 30 seconds
| Where the insert runs | Most likely cause |
|---|---|
| Edge function, API route or webhook using the service role key | The server does not send the owner |
| The browser, and the form sends only its own fields | The app does not send the owner |
| Sign-up fails with “Database error saving new user” | A sign-up trigger leaves a column empty |
| The browser, but you see “new row violates row-level security policy” | That is the row level security error: the insert policy caught the empty owner first |
We reproduced every cause on Postgres with Supabase’s roles before writing this page. The table has an owner column, row level security and a policy that lets users read their own notes:
create table public.notes (
id uuid primary key default gen_random_uuid(),
user_id uuid not null references auth.users (id) on delete cascade,
body text not null,
created_at timestamptz not null default now()
);
alter table public.notes enable row level security;
create policy "Users can read their notes"
on public.notes for select
to authenticated
using ((select auth.uid()) = user_id);The three causes
1. A server insert without the owner
Edge functions, API routes and Stripe webhooks often write with the service role key. That key bypasses row level security, so no policy stops the insert, and NOT NULL is the only check left. If the code builds the row from the request and forgets user_id, this error is the result. A default of auth.uid() does not help here: with the service role key there is no signed-in user, so auth.uid() is null.
2. The app does not send the owner
The form sends body and nothing else. If the table had an insert policy that compares auth.uid() with user_id, you would see the row level security error instead, because Postgres checks the policy before NOT NULL. So when the browser gets the not-null message, the table either has RLS switched off or an insert policy that does not look at the owner, like this one:
create policy "Signed-in users can add notes"
on public.notes for insert
to authenticated
with check (auth.uid() is not null);This policy is a second problem. It lets any signed-in user insert notes with someone else’s user_id. We checked: with it in place, one user can create rows in another user’s name.
3. A sign-up trigger leaves a column empty
Many apps create a profile row for every new user with a trigger on auth.users. If the profile table has a required column that the trigger does not fill, every sign-up fails. Supabase Auth reports only Database error saving new user; the not-null message appears in the Auth and Postgres logs.
create table public.profiles (
id uuid primary key references auth.users (id) on delete cascade,
username text not null,
full_name text
);
create function public.handle_new_user()
returns trigger
language plpgsql
security definer set search_path = ''
as $$
begin
insert into public.profiles (id, full_name)
values (new.id, new.raw_user_meta_data ->> 'full_name');
return new;
end;
$$;
create trigger on_auth_user_created
after insert on auth.users
for each row execute function public.handle_new_user();How to fix it
For writes from the browser, let the database fill in the owner and make the insert policy check it. This fixes cause 2 and closes the hole in the loose policy:
alter table public.notes alter column user_id set default auth.uid();
drop policy if exists "Signed-in users can add notes" on public.notes;
create policy "Users can add their own notes"
on public.notes for insert
to authenticated
with check ((select auth.uid()) = user_id);The app no longer sends user_id at all, so it cannot send an empty or wrong one.
For cause 1, send the owner explicitly on the server, and take it from the verified session, never from the request body. Our page on edge functions that trust the user id in the body explains why:
// token = the caller's access token from the Authorization header
const { data: { user } } = await supabase.auth.getUser(token);
if (!user) return new Response("Unauthorized", { status: 401 });
await admin.from("notes").insert({ body, user_id: user.id });For cause 3, make the trigger fill every required column, with a fallback when the sign-up form did not send a value:
create or replace function public.handle_new_user()
returns trigger
language plpgsql
security definer set search_path = ''
as $$
begin
insert into public.profiles (id, username, full_name)
values (
new.id,
coalesce(new.raw_user_meta_data ->> 'username', 'user_' || left(new.id::text, 8)),
new.raw_user_meta_data ->> 'full_name'
);
return new;
end;
$$;For a column that is truly optional, like full_name here, allowing null is fine. The owner column is not optional.
My Supabase app fails with “null value in column user_id of relation your_table violates not-null constraint”. Find every place that inserts into that table: browser code, edge functions, API routes, webhooks and triggers on auth.users. Then fix it like this, in a new migration and in the code: make the owner column default to auth.uid(), replace any insert policy that does not compare (select auth.uid()) with the owner column by one that does, for the authenticated role only. In server code that uses the service role key, set the owner explicitly from the verified session user, never from the request body. In sign-up triggers, fill every not-null column, with a fallback value where the metadata may be missing.
Do not drop NOT NULL on the owner column, do not add policies with true or “owner is null”, do not disable RLS and do not edit existing migrations. Show me the migration and the code change before applying them.
Check that the fix works
In the SQL Editor, act as a signed-in user inside a transaction. Use a real user id from Authentication → Users:
begin;
set local role authenticated;
set local request.jwt.claims = '{"sub": "11111111-1111-1111-1111-111111111111", "role": "authenticated"}';
insert into public.notes (body) values ('Hello') returning id, user_id;
rollback;The insert returns one row whose user_id is that id, filled in by the database. Now change the insert to (body, user_id) with values ('Hello', '22222222-2222-2222-2222-222222222222'), the id of another user: it must fail with the row level security error. For the trigger, create a test user under Authentication → Users and check that a profile row appears. The guide to testing RLS policies has the full set of checks to run before launch.
Frequently asked questions
Can I just remove NOT NULL from the column?
Not from an owner column like user_id. Rows without an owner vanish for everyone under normal owner policies, and the usual next fix, a policy that also allows rows where user_id is null, shows them to every signed-in user. Fill the column instead. For optional columns such as a display name, allowing null is fine.
Why do I get the row-level security error instead of this one?
Postgres checks the insert policy before NOT NULL. If your insert policy compares auth.uid() with user_id, an empty user_id fails the policy first. Seeing the not-null message from the browser means your insert policy does not check the owner, which is worth fixing too.
I set the column default to auth.uid(). Why does my edge function still fail?
auth.uid() reads the signed-in user from the request. With the service role key there is no user, so the default is null. On the server, take the user id from the verified session and send it explicitly.
Sign-up fails with Database error saving new user. Is that the same error?
Often, yes. Supabase Auth hides the database error behind that generic message. Open the Auth logs or the Postgres logs in the dashboard; if a trigger on auth.users left a profile column empty, you will find this not-null message there.
Sources
- 1.Constraints — PostgreSQL Documentation
- 2.PostgreSQL Error Codes — PostgreSQL Documentation
- 3.User Management — Supabase Docs
- 4.Database error saving new user — Supabase Docs
- 5.Row Level Security — Supabase Docs