© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
SupabaseS
Supabase•4y ago•
4 replies
Jamie

Race condition with RLS

I have somewhat of a race condition but I'm not sure the exact execution order inside postgres.

I have
teams
teams
and
team_members
team_members
tables.

teams
teams
has an RLS policy for SELECT statements meaning only members of that team can read it (if currently authed user is present in the
team_members
team_members
table). There's also a similar policy on
team_members
team_members
.

I allow any
authenticated
authenticated
user to INSERT into
teams
teams
, and then a trigger will add a row to
team_members
team_members
with the user that created the team. Simple enough.

The issue is this insert fails because of the RLS policy - because I am returning a result from the modified row, my assumption being that when I
.select()
.select()
from the insert - the trigger has not yet been run and therefore no record exists in
team_members
team_members
.

If I comment out these lines
const res = await supabase
                .from('teams')
                .insert({
                    name: name,
                })
                .select() // <<< delete
                .single(); // <<< delete
const res = await supabase
                .from('teams')
                .insert({
                    name: name,
                })
                .select() // <<< delete
                .single(); // <<< delete


The insert works correctly - but I do need to know the ID of the team I just created for the app flow to work correctly.

Any thoughts?
Supabase banner
SupabaseJoin
Supabase gives you the tools, documentation, and community that makes managing databases, authentication, and backend infrastructure a lot less overwhelming.
45,816Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

RLS Condition check
SupabaseSSupabase / help-and-questions
4y ago
Race condition in insert
SupabaseSSupabase / help-and-questions
6mo ago
Caching via Race Condition?
SupabaseSSupabase / help-and-questions
3y ago
Supabase Realtime race condition
SupabaseSSupabase / help-and-questions
4y ago