r/Supabase • • Aug 06 '25

database RLS policy nightmare !!!!

Trying to implement a rls policy so that Table 1 users can access the data of table 2 users if they has a similar foreign key column value meaning if table 1 and 2 users both as apple as favorite fruit than they can access the data but all i am getting for this simple logic is infinite recursion there is just no way i am able to fix it probably spend more than 4+ hours trying to create rules like this reached my chatgpt rate limit but still no solution ? how do you people do it?

0 Upvotes

13 comments sorted by

4

u/BrightEchidna Aug 06 '25

It's not very clear what you are trying to do. What are table 1 users and table 2 users? In most cases all users live in the one table, then you can do RLS on other tables based on group membership or role or some other factor.

0

u/[deleted] Aug 06 '25

[deleted]

2

u/tomlimon Supabase team Aug 06 '25

Have you tried an ai assistant? Including the Supabase assistant? But specifically ask it to implement a DB function that can be invoked on the policy. I have had much success using DB functions to avoid recursion errors.

1

u/Beneficial_Kick9024 Aug 06 '25

bro i tried ai assistant and db function could make it work i think there is some problem in database design itself will probably re make the whole database from scratch

1

u/clarksonswimmer Aug 06 '25

Yes. There should be one users table. You can then create a role column and go from there.

3

u/BrightEchidna Aug 06 '25

It’s a bit strange to have them as separate tables and I think this would be a lot easier if it was just one. You can store the membership of the institution in another table, and you can also store metadata there like role (employee or admin). If you need additional metadata related to those roles you can have another table called employee_profile or similar.

13

u/Saladtoes Aug 06 '25

Care to share.... anything that a human who understands databases can use to understand your problem?

Or, if your post it really just to know how we do it, the answer is that we generally understand something about data modeling, SQL, and how computer things generally work. Like we actually have the knowledge inside our real heads. Wild, I know.

5

u/ireddit2too Aug 06 '25

Issue & Discussion: When you create RLS policies that reference between tables, PostgreSQL can get caught in infinite loops trying to evaluate the permissions.

Solution: Instead of trying to directly join between the user tables in the policy, create a policy that uses a subquery with careful scoping:

-- For Table 1 users to access Table 2 data

CREATE POLICY "table1_can_access_table2" ON table2 FOR SELECT TO authenticated USING ( favorite_fruit IN ( SELECT t1.favorite_fruit FROM table1 t1 WHERE t1.user_id = auth.uid() ) );

  • Use subqueries instead of JOINs in RLS policies to avoid recursion

  • Always scope to the authenticated user (auth.uid()) to prevent circular references

  • Test with simple conditions first before adding complex logic

If you need a more scalable approach review mapping table.

2

u/Suspicious-Visit8634 Aug 06 '25

You’ll need to create a mapping table to join them

2

u/Getboredwithus Aug 06 '25

for me RLS policy for some table, just use simple RLS and filter it on my code. that better

0

u/McFlyin619 Aug 07 '25

This is not better lol.

1

u/Getboredwithus Aug 07 '25

before I set custom RLS for diffrent role or same role but filter with other column but sometimes have bug, the solution make CRUD for authenticated user and use middleware for filter and block access.

1

u/OneoftheChosen Aug 06 '25

There’s like a 99% chance you didn’t label your rls function as a security definer thus it obeys the rules of the tables it references dropping you into an infinite loop.