J0mk
J0mk

Reputation: 143

supabase database functions and RLS

I am using supabase and i am trying to accomplish one thing. I have a table called "customers", and it's like this: customer_id, first_name, last_name,....., profile_id (from the profile table, where data of users is stored). I have setup a simple RLS that let me do operations on this table that is as follow: (auth.id() = profile_id). In this way a user can see only its customers and its working fine. Now i have a "contracts" table.. This table has a relationship with "customers" table, every customer can have multiple contracts. I want to be able to select every contract from the contract table, only if the corresponding customer has a field that correspond to profile_id. Is the same as the first RLS rule on the customer table, but i need it to use it on the contracts table and avoid inserting profile_id also on this table. I'm new to postgres and supabase, can someone show me how it's done? Thanks

Upvotes: 0

Views: 440

Answers (1)

J0mk
J0mk

Reputation: 143

(EXISTS ( SELECT 1 FROM customers WHERE ((customers.customer_id = contracts.customer_id) AND (customers.point_id = auth.uid()))))

Upvotes: 0

Related Questions