Skip to main content
NikoFischer.com

Main navigation

  • Home
  • About
    • Recommended Youtube Channels
    • Life Rules
    • Podcast
  • 50-Day Challenge
  • Impressum
Sprachumschalter
  • English

Breadcrumb

  1. Home

Supabase throws "new row violates row-level security policy for table" even though I had created a row level policy for inserts

I was confronted with a Supabase problem that (almost) drove me crazy: I have a simple table "projects" that only stores a project uuid and a project name. While developing on my Web-App I disabled role level security (RLS) for this table. I usually proceed disabling row level policies in the first phase of developing as it helps me to focus on the frontend and the general project scheme.

Inserting data in the table throws a row-level security alert

As I enabled row-level security on my "projects" table and trying to insert new data I received the following error:

new row violates row-level security policy for table "projects"

This is how my policy looks like:

Supabase row level security policy

This is a simple rule that checks if the user is authenticated while inserting new data. I took the rule from the example rules provided by the Supabase Team itself.

Solution: Add a SELECT policy

I found out that Supabase (or Postgres?) seems to not only INSERT the data you provide but also directly tries to SELECT it. So if you don't have a SELECT policy also the INSERT process fails.

In my case this is not quite easy to solve: I have another table called project_members where I reference all users that are related to the project. So at first time when inserting new data to the projects table there is no data in the project_members table. This leads to a situation where the SELECT policy for my projects table always fails on the first INSERT.

But fortunately there is another way:

Solution 2: Modify insert statement

Just add { returning: 'minimal' } to your insert statement. This suppresses the direct selection of the added data. The complete statement looks like this:

await supabase.from('sample').insert({ key: 'value' }, { returning: 'minimal' })

This works perfectly!

Tags

  • Supabase
  • Add new comment

Comments2

About text formats

Restricted HTML

  • Allowed HTML tags: <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

Royal VICTOR (not verified)

3 months 2 weeks ago

Thanks !!

This bug drives me crazy too and I spent a lot of time before discovering your article !
Nice job ! Thank you !!

  • Reply

Lautixon (not verified)

1 month 1 week ago

Thanks😊

This worked perfectly

  • Reply

Related articles

Supabase: How to query users table?
Supabase vs. Firebase - Unveiling the Differences
Ihre Anmeldung konnte nicht gespeichert werden. Bitte versuchen Sie es erneut.
Ihre Anmeldung war erfolgreich.

Newsletter

Join a growing community of friendly readers. From time to time I share my thoughts about rational thinking, productivity and life.

Nikolai Fischer

✌ Hi, I'm Niko
Entrepreneur, developer & podcaster

Contact me:

  • E-Mail
  • Phone
  • LinkedIn
RSS feed