Symptom: insert blocked by RLS
Supabase error: “new row violates row-level security policy”
Reading data works, but the moment your app tries to save something (a form, a new profile, a file upload) it fails with new row violates row-level security policy for table "…". The AI adds a retry, renames a column, and the error stays.
Why this happens
Row-level security (RLS) is Supabase’s permission system. Once it’s on, every insert has to pass a policy, or Postgres refuses it. The error isn’t a code bug, it’s a missing or mismatched permission. Four causes cover almost every case:
- There is no INSERT policy. The AI wrote a SELECT policy so reads work, but never wrote one that allows inserts.
- The policy’s check fails. A typical policy says
user_id = auth.uid(). If the app doesn’t senduser_id, or sends the wrong value, the row fails the check. - The user isn’t signed in when the insert runs. If the request goes out before the session loads, it runs as the
anonrole, and policies written forauthenticatedusers don’t apply. - The insert asks for the row back. Calling
.insert(...).select()also needs a SELECT policy that lets the user see the new row. File uploads hit the same error on Supabase Storage, which has its own policies onstorage.objects.
Step 1: find which one it is
- In the Supabase dashboard, open Authentication → Policies and find the table named in the error. Is there a policy for INSERT (or ALL)?
- If there is, read its check. Note which column it compares to
auth.uid(), then confirm your app actually sends that column with the right value. - In the browser’s Network tab (F12), click the failed request. If there’s no
Authorization: Bearerheader with a user token, the user wasn’t signed in when it ran. - If the error comes from a file upload, check the policies on the storage bucket, not the table.
Don’t fix this by turning RLS off. That makes the error disappear by letting anyone with your public key read and change every row.
Step 2: have the AI fix the policy, not the code
Paste this into your AI tool and fill in the brackets:
Inserting into my Supabase table [table name] fails with: "new row violates row-level security policy for table [table name]" It happens when: [the action, e.g. "submitting the signup form" or "uploading an avatar"]. Do not disable RLS and do not change unrelated code. 1. Show me the current RLS policies on this table (or tell me the SQL to list them). 2. Check whether there is an INSERT policy and what its WITH CHECK condition is. 3. Show me the exact insert call in my code and which columns it sends. Does it set the column the policy checks (e.g. user_id = auth.uid())? 4. Confirm the user is signed in before this insert runs. 5. If the insert uses .select() afterwards, check for a matching SELECT policy. Then give me the single smallest fix: either the SQL for the missing or corrected policy, or the one-line code change. Explain why it was failing in two sentences.
Why it works: it stops the AI from guessing in your app code and points it at the permission rule that rejected the row, while ruling out the unsafe “just disable RLS” shortcut.
Still going in circles on Supabase errors?
The free 2-Strike Rule card tells you when to stop prompting and what to paste in a fresh chat so the AI stops guessing. One page, keep it next to your editor.
Free via Gumroad: enter $0 and your email at checkout.
Related symptoms
If inserts work but your tables come back empty, read Supabase RLS returning empty data in Lovable. If it only breaks on the live site, see App works in preview but blank after deploy.