Fix “new row violates row-level security policy” (Supabase error 42501)

By AI App Medic · Updated · Supabase · RLS · Bolt · Lovable

You click "Save" in your Lovable or Bolt app and the console shows:

{ "code": "42501", "message": "new row violates row-level security policy for table \"projects\"" }

Supabase is telling you that Row Level Security is enabled on the table and none of its policies allow this specific row to be written by the current user. Here are the causes in the order we see them most.

Cause 1: there is no INSERT policy at all

Enabling RLS denies everything until you add policies. A SELECT policy does not allow inserts. Check:

select policyname, cmd, roles, with_check
from pg_policies
where schemaname = 'public' and tablename = 'projects';

If nothing has cmd = INSERT (or ALL), add one:

create policy "Users can create their own projects"
  on public.projects for insert to authenticated
  with check ((select auth.uid()) = user_id);

Cause 2: the request is not authenticated

If the policy says to authenticated but the request runs before the session is loaded (or after it expired), it runs as anon and fails. In DevTools > Network, open the failing request: an authenticated request carries an Authorization: Bearer header whose token belongs to the user, not just your public key. Make sure your code waits for supabase.auth.getSession() (or the auth state listener) before writing.

Cause 3: user_id does not match auth.uid()

The policy compares the row's user_id with the signed-in user's id. If the frontend sends a different value, a profile id, or nothing at all, the check fails. The lasting fix is to stop sending it and let Postgres fill it:

alter table public.projects
  alter column user_id set default auth.uid();

Then remove user_id from the object you pass to .insert().

Cause 4: insert().select() or upsert() without a SELECT policy

When your code asks for the inserted row back, Supabase also needs permission to read it. Without a SELECT policy the insert can fail with the same 42501 message. Add:

create policy "Users can read their own projects"
  on public.projects for select to authenticated
  using ((select auth.uid()) = user_id);

upsert() may update an existing row, so it needs INSERT, UPDATE and SELECT policies.

Cause 5: it is actually a Storage upload

If the error appears when uploading a file, the table in question is storage.objects. Storage buckets have their own policies. A typical per-user pattern stores files under a folder named after the user id:

create policy "Users upload to their own folder"
  on storage.objects for insert to authenticated
  with check (
    bucket_id = 'avatars'
    and (storage.foldername(name))[1] = (select auth.uid())::text
  );

Your upload path then has to start with the user's id, for example `${user.id}/avatar.png`.

What not to do

After the fix, test with two accounts and while signed out. The full RLS troubleshooting guide covers the read side (empty lists) too. If the error text is infinite recursion detected in policy rather than 42501, see how to break a recursive policy. If it says permission denied for table with a GRANT hint, no policy will help, because the table has no grant for that role: see permission denied for table on a new table.

FAQ

What does error code 42501 mean in Supabase?

42501 is Postgres' insufficient-privilege error. With RLS it usually means no INSERT (or UPDATE) policy allowed the row you tried to write for the current role.

I added an INSERT policy and still get the error. Why?

Common reasons: the request is made while signed out (anon role) but the policy is only for authenticated; the user_id you send does not equal auth.uid(); or you call insert().select() and there is no SELECT policy.

Can I fix it by using the service role key in the frontend?

No. The service role key bypasses RLS entirely; shipping it to the browser gives every visitor full read/write access to your database.

Related guides