Given a Supabase table with the following insert policy, what will happen when a user tries to insert a new row?
CREATE POLICY "Allow insert if user is owner" ON todos FOR INSERT USING (auth.uid() = owner_id);
Think about what the USING clause checks during an insert operation.
The USING clause in an insert policy checks the condition against the new row being inserted. Here, it ensures the authenticated user's ID matches the owner_id field in the new row, allowing the insert only if true.
Which of the following Supabase policies correctly allows authenticated users to insert rows only if the user_id column matches their own ID?
Remember that WITH CHECK is used to validate new rows on insert.
For insert operations, WITH CHECK validates the new row's data. The condition user_id = auth.uid() ensures users can only insert rows where the user_id matches their own ID.
You have a multi-tenant application where each tenant has a unique tenant_id. You want to allow users to insert data only for their own tenant. Which policy correctly enforces this?
Consider how to compare tenant_id with a session variable during insert.
The WITH CHECK clause validates the new row's tenant_id against the current tenant stored in a session variable. This ensures users can only insert rows for their own tenant.
What happens if a Supabase table has no insert policies defined and RLS (Row Level Security) is enabled?
Think about the default behavior of RLS when no policies exist.
When RLS is enabled and no insert policies exist, all insert operations are denied by default to protect data security.
You want to create an insert policy that allows users to insert rows only if they belong to a specific role and the data meets a complex condition involving multiple columns. Which approach is best?
Consider how Supabase evaluates multiple policies and clauses.
A single WITH CHECK clause combining all conditions ensures atomic validation of the new row on insert. Multiple policies can cause unexpected behavior because all must allow the insert.
