question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to delete `auth.users` row due to `objects_owner_fkey` FK constraint

See original GitHub issue

Bug 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

  1. Create a user
  2. Authenticate as that user on the client
  3. Upload an object as that user on the client
  4. Delete that user via dashboard
  5. 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:open
  • Created 2 years ago
  • Reactions:5
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
mcewen87commented, Aug 17, 2021

Hi @inian

As I understand, the solution to this is to add this SQL

drop constraint objects_owner_fkey,
add constraint objects_owner_fkey
   foreign key (owner)
   references auth.users(id)
   on delete set null;

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?

1reaction
iniancommented, Aug 12, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found