如果用户将徽章设为其标题徽章,则无法删除该徽章

我猜这又是另一个损坏的表问题?不过这种情况发生在一个不太可能非正常关闭数据库的实例上。

我创建并删除了一个没有成员的徽章,操作正常。我将一个用户添加到新徽章并删除它,也正常。

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".

但我不太清楚该从哪里入手,或者尝试重建哪个索引。

1 个赞

TABLE "user_profiles" CONSTRAINT "fk_rails_38ea484ed4" FOREIGN KEY (granted_title_badge_id) REFERENCES badges(id)

这条 SELECT * FROM user_profiles where granted_title_badge_id = 118; 语句会告诉你什么?

4 个赞

这给了我两个用户。
然后我执行了

delete  FROM user_profiles where granted_title_badge_id = 118;

之后就可以删除该徽章了。

我的问题似乎已经解决,至少对于这个徽章是这样。

但删除徽章难道不应该自动完成这些操作吗?现在看起来更像是个 :bug:

2 个赞

:screech: 你刚刚删除了两个用户的个人资料 :screech:

下次

UPDATE user_profiles SET granted_title_badge_id = (另一个 ID) WHERE granted_title_badge_id = 118

4 个赞

天哪。这确实是个问题。所以我一直告诉大家不要在 SQL 中直接修改数据库。

好在只涉及 2 个用户。也许我还能滚动页面看看他们是谁。

但无论如何,如果某人拥有某个徽章就无法删除它,这看起来仍然是一个漏洞。

将其重新归类为漏洞。

3 个赞

不。看起来像是一个(次要的)漏洞,即当有人拥有某个徽章时,你却可以发起删除该徽章的操作。如果用户界面能阻止这种情况,那就更好了。数据库阻止删除并维护参照完整性是完全正确的。

2 个赞

同意保留徽章比让系统处于损坏状态更好,但目前它返回 500 错误且没有任何说明。

这确实是一个轻微的用户体验问题,但它会阻止那些不如你了解系统的人删除徽章。:slight_smile:

非常感谢你的帮助。我正在等待看看我删除的那些用户的个人资料会发生什么!

3 个赞

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.