https://supabase.com/ logo
hey guys, I'm trying to figure out some RLS Policy...
# help
m
hey guys, I'm trying to figure out some RLS Policy. Basically I'm making a somewhat simple app to organize my own finances. - Spaces: which space that expense belongs to (like office, home, etc) - Expenses - Category - Profiles **What I want to accomplish**: I want users to be able to see other users profiles IF they belong to the same space. Like, I'm in the home space, and add my partner to this space, we should be able to see each other profile. Any help is much appreciated
n
Hello @mathewcst! This thread has been automatically created from your message in #843999948717555735 a few seconds ago. We have already mentioned the @User so that they can see your message and help you as soon as possible! Want to unsubscribe from this thread? Right-click the thread in Discord (or use the ``...`` menu) and select "Leave Thread" to unsubscribe from future updates. Want to change the title? Use the ``/title`` command! We have solved your problem? Click the button below to archive it.
o
Hi you easily do it using a Many to Many relationship. Each user has multiple spaces, and each space has multiple users @mathewcst Right now you're using an array, which is not ideal in your situation
m
@Olyno do you have some example I can look at? I'm not that good with database stuff xD
o
You create a table which reference 2 column (foreign keys): 1 column for the user id, and 1 for the space id. When you want to make a check, just check if the space id is the same between users
m
That's exactly what I tried to do. My
profiles
table has
user_id
and
spaces
(which is an array of space_id) my
spaces
table has a
users
column (which is an array of user_id)
Now the RLS policy to compare if two users are in the same space is where I'm having trouble. Tried to do this into the
profiles
RLS
Copy code
exists (
    select id from space
    where profiles.spaces = ANY(space.id)
  )
o
Indeed, it could work, even if it's not the best practice
This RLS doesn't work?
m
unfortunately not
oh just found out about this many to many relationship. Thanks a lot @Olyno for the help
n
Thread was archived by @mathewcst. Anyone can send a message to unarchive it.