Unable to delete `auth.users` row due to `objects_owner_fkey` FK constraint
See original GitHub issueBug report
Describe the bug
When a user uploads an object to a bucket, the object’s row in storage.objects
has a column owner
that has a FK constraint objects_owner_fkey
to auth.users.id
. However, it’s not set up with on delete {cascade|set null}
—which prevents the user from actually being deleted.
Attempting to delete a user with a storage.object
referencing the user results in a FK constraint violation.
To Reproduce
- Create a user
- Authenticate as that user on the client
- Upload an object as that user on the client
- Delete that user via dashboard
- You’ll get a FK constraint violation error:
Deleting user failed: update or delete on table “users” violates foreign key constraint “objects_owner_fkey” on table “objects”
Expected behavior
Should be able to delete user whilst retaining the object in the database.
Suggested fix & temporary workaround
Add on delete set null
to the objects_owner_fkey
constraint:
alter table storage.objects
drop constraint objects_owner_fkey,
add constraint objects_owner_fkey
foreign key (owner)
references auth.users(id)
on delete set null;
Issue Analytics
- State:
- Created 2 years ago
- Reactions:5
- Comments:12 (4 by maintainers)
Top Results From Across the Web
Can't delete user due to foreign key restraint - Site Operators
I was able to use this command to remove a user: tutor local run lms ./manage.py lms manage_user --remove <user_name> <user_email>.
Read more >can't delete object due to foreign key constraint - Stack Overflow
I'm trying to do a simple user.destroy but running into the following error: ERROR: update or delete on table "users" violates foreign key...
Read more >Cannot Delete a User From Crowd due to Foreign Key ...
BatchUpdateException: Cannot delete or update a parent row: a foreign key constraint fails (`auth`.`cwd_user_credential_record`, CONSTRAINT ...
Read more >Operating without foreign key constraints - PlanetScale
Possibly the single most important feature of foreign keys, a DELETE on a parent will fail if child rows exist that reference the...
Read more >MySQL | Deleting rows when there is a foreign key
If ON DELETE CASCADE constraint is not used then referencing error occurs. Step-7: Deleting rows when there is a foreign key : Query...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hi @inian
As I understand, the solution to this is to add this SQL
Is that correct?
Additionally, can you share some insight into what is going on behind the scenes to create this restriction, and why it is in place?
Thats a good point Jian. I will add a trigger to make the owner as null in storage.objects when a row is deleted from auth.users.