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

**URL:** <https://meta.discourse.org/t/cant-delete-badge-if-a-user-has-it-as-their-title-badge/153121>\
**Category:** Bug\
**Created:** [2020 年5 月 28 日 16:59 UTC](https://meta.discourse.org/t/cant-delete-badge-if-a-user-has-it-as-their-title-badge/153121 "2020-05-28T16:59:08Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

作者： ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)\
发布日期： [2020 年5 月 28 日 16:59 UTC](https://meta.discourse.org/t/cant-delete-badge-if-a-user-has-it-as-their-title-badge/153121/1 "2020-05-28T16:59:09Z")

</div>

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

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

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

```

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

---

_[View the full topic](https://meta.discourse.org/t/cant-delete-badge-if-a-user-has-it-as-their-title-badge/153121)._
