The problem is that a badge cannot be deleted if a user has it as their title badge. pfaffman encountered this issue and received a 500 error with a PG::ForeignKeyViolation error message when trying to delete the badge. RGJ pointed out that the error occurs because the badge ID is still referenced in the user_profiles table.
To resolve the issue, pfaffman deleted the referencing rows in the user_profiles table, allowing the badge to be deleted. However, RGJ warned that this approach deleted the profiles of two users and recommended updating the granted_title_badge_id instead.
RGJ and pfaffman agreed that the issue is a UX bug, as the user interface allows the deletion of a badge even if it’s referenced in another table, and returns a 500 error without explanation. The correct behavior would be to prevent the deletion of the badge through the user interface to maintain referential integrity.
I guess this is another corrupt table issue? Though this is on an instance where it is unlikely that the database was shut down uncleanly.
I created and deleted a badge with no members and that worked fine. I added a user to a new badge and deleted it and that worked fine.
Started DELETE "/admin/badges/118" for 71.89.242.121 at 2020-05-28 16:50:04 +0000
Processing by Admin::BadgesController#destroy as */*
Parameters: {"id"=>"118"}
Rendering text template
Rendered text template (Duration: 0.0ms | Allocations: 1)
Completed 500 Internal Server Error in 304ms (ActiveRecord: 0.0ms | Allocations: 114838)
ActiveRecord::InvalidForeignKey (PG::ForeignKeyViolation: ERROR: update or delete on table "badges" violates foreign key constraint "fk_rails_38ea484ed4" on table "user_profiles"
DETAIL: Key (id)=(118) is still referenced from table "user_profiles".
)
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-mini-profiler-2.0.1/lib/patches/db/pg.rb:72:in `exec_params'
Failed to handle exception in exception app middleware : PG::ForeignKeyViolation: ERROR: update or delete on table "badges" violates foreign key constraint "fk_rails_38ea484ed4" on table "user_profiles"
DETAIL: Key (id)=(118) is still referenced from table "user_profiles".
But I don’t quite know where to look or what index to try to rebuild.
No. It seems like a (minor) bug that you can initiate the deletion of a badge if someone has it. It would be nicer if the user interface would prevent it. It is totally correct that the database prevents deletion and preserves referential integrity.