What if a simple rule could stop accidental data loss and keep your app safe without extra work?
Why Update and delete policies in Supabase? - Purpose & Use Cases
Imagine you have a shared online notebook where many friends can write and erase notes. Without clear rules, anyone might accidentally delete or change important notes, causing confusion and lost information.
Manually checking who can update or delete each note is slow and risky. Mistakes happen easily, like deleting the wrong note or letting someone change notes they shouldn't. This wastes time and can break trust.
Update and delete policies set clear, automatic rules about who can change or remove data. These rules run behind the scenes, so only the right people can make updates or deletions, keeping data safe and organized without extra effort.
Allow all users to update or delete any record without checksCreate policies that let users update or delete only their own recordsIt enables secure, automatic control over who can change or remove data, protecting your app's information effortlessly.
In a task app, update and delete policies ensure users can only edit or remove their own tasks, preventing others from messing with their work.
Manual control over updates and deletes is slow and error-prone.
Policies automate who can change or remove data safely.
This keeps your app's data secure and trustworthy.
Practice
UPDATE policy in Supabase?Solution
Step 1: Understand the role of update policies
Update policies define who is allowed to modify existing rows in a table.Step 2: Differentiate from other policies
Read policies control reading, insert policies control adding data, and table creation is unrelated.Final Answer:
To control who can change existing data in a table -> Option CQuick Check:
Update policy = control data changes [OK]
- Confusing update with read or insert policies
- Thinking update policies control table creation
- Assuming update policies allow adding new rows
Solution
Step 1: Identify keywords for policy conditions
Supabase usesUSINGto specify who can perform an action andWITH CHECKto add extra conditions on updates.Step 2: Match keyword to extra conditions on update
WITH CHECKensures the new data meets certain rules before update is allowed.Final Answer:
WITH CHECK -> Option AQuick Check:
Extra update conditions = WITH CHECK [OK]
- Confusing USING and WITH CHECK keywords
- Thinking WHERE is used in policies
- Assuming ALLOW is a valid keyword
CREATE POLICY "Update own profile" ON profiles FOR UPDATE USING (auth.uid() = profiles.user_id) WITH CHECK (auth.uid() = profiles.user_id);
What happens if a user tries to update another user's profile?
Solution
Step 1: Understand USING condition role
TheUSINGclause controls who can perform the update. It checks if the user's ID matches the profile's user_id.Step 2: Check condition for updating another user's profile
If a user tries to update a different profile,auth.uid() = profiles.user_idis false, so update is denied.Final Answer:
The update is denied because USING condition fails -> Option DQuick Check:
USING controls update permission = denied if false [OK]
- Thinking WITH CHECK controls permission to update
- Assuming USING applies only to deletes
- Ignoring that both USING and WITH CHECK must pass
CREATE POLICY "Delete own posts" ON posts FOR DELETE USING (user_id = auth.uid());
But users report they cannot delete their posts. What is the likely problem?
Solution
Step 1: Check policy syntax
The policy syntax is correct for delete with USING condition matching user_id.Step 2: Consider RLS status
If Row Level Security (RLS) is not enabled on the table, policies have no effect and all actions are denied by default.Final Answer:
The policy might be missing enabling RLS on the table -> Option BQuick Check:
RLS must be enabled for policies to work [OK]
- Confusing USING and WITH CHECK for delete policies
- Forgetting to enable RLS on the table
- Thinking FOR UPDATE is needed for delete policies
Solution
Step 1: Understand USING vs WITH CHECK
USINGcontrols who can perform the update;WITH CHECKensures the new data meets conditions.Step 2: Apply condition on new data
The status check must apply to the new value after update, so usenew.status != 'shipped'inWITH CHECK.Step 3: Verify options
FOR UPDATE USING (auth.uid() = orders.user_id) WITH CHECK (new.status != 'shipped') correctly usesUSING (auth.uid() = orders.user_id)andWITH CHECK (new.status != 'shipped').Final Answer:
FOR UPDATE USING (auth.uid() = orders.user_id) WITH CHECK (new.status != 'shipped') -> Option AQuick Check:
Use WITH CHECK with new. for update conditions [OK]
- Checking old data instead of new data in WITH CHECK
- Putting status condition in USING instead of WITH CHECK
- Forgetting to use new. prefix for updated row
Ready to prove it?
Pass the test and this pattern moves unknown
