# ActiveRecord::RecordNotUnique on idx\_tag\_users\_ix1：如何修复？

**URL:** <https://meta.discourse.org/t/activerecord-recordnotunique-on-idx-tag-users-ix1-how-to-fix/287078>\
**Category:** Support\
**Created:** [2023年十一月30日 14:18 UTC](https://meta.discourse.org/t/activerecord-recordnotunique-on-idx-tag-users-ix1-how-to-fix/287078 "2023-11-30T14:18:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hellekin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/hellekin/32/51636_2.png) [@hellekin](https://meta.discourse.org/u/hellekin)\
**Post date:** [2023年十一月30日 14:18 UTC](https://meta.discourse.org/t/activerecord-recordnotunique-on-idx-tag-users-ix1-how-to-fix/287078/1 "2023-11-30T14:18:42Z")

</div>

继续讨论 [SSO 损坏 - 找不到请求的 URL 或资源](https://meta.discourse.org/t/sso-broken-the-requested-url-or-resource-could-not-be-found/231230/9)：

> [@davidkingham](#):
>
> ActiveRecord::RecordNotUnique (PG::UniqueViolation: ERROR: duplicate key value violates unique constraint “idx\_tag\_users\_ix1” DETAIL: Key (user\_id, tag\_id, notification\_level)=(8118, 842, 0) already exists. )

我遇到了类似的问题：

```plaintext
ActiveRecord::RecordNotUnique (PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "idx_tag_users_ix1"

```

有什么办法可以删除重复的键吗？

---

<div class="post-metadata">

**Author:** ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)\
**Post date:** [2023年十一月30日 14:23 UTC](https://meta.discourse.org/t/activerecord-recordnotunique-on-idx-tag-users-ix1-how-to-fix/287078/2 "2023-11-30T14:23:27Z")

</div>

我认为这是因为您有大小写混合的标签（您有 `tag` 和 `Tag`）。

这是在重建时发生的还是在其他时间发生的？

我认为应该做的是尝试重建索引，然后修复标签的大小写（如果我猜得没错），或者删除其中一个重复的条目。

---

<div class="post-metadata">

**Author:** ![hellekin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/hellekin/32/51636_2.png) [@hellekin](https://meta.discourse.org/u/hellekin)\
**Post date:** [2023年十一月30日 14:28 UTC](https://meta.discourse.org/t/activerecord-recordnotunique-on-idx-tag-users-ix1-how-to-fix/287078/3 "2023-11-30T14:28:36Z")

</div>

嗯…… 事实并非如此。

```plaintext
Tag.all.pluck(:name)
=> ["welcome",
 "faq",
 "sysadmin",
 "offdem-0x03",
 "reference",
 "tutorial",
 "local-organization",
 "collective-data",
 "power-relations",
 "offdem-topic",
 "offdem-0x00",
 "offdem-0x01",
 "offdem-0x02",
 "discourse-plugin",
 "page",
 "how-to",
 "about",
 "offdem-0x04"]

```

> [@pfaffman](#):
>
> Is this happening on a rebuild or some other time?

据推测，它出现在本月早些时候。我想是在升级之后。

---

<div class="post-metadata">

**Author:** ![hellekin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/hellekin/32/51636_2.png) [@hellekin](https://meta.discourse.org/u/hellekin)\
**Post date:** [2023年十一月30日 14:38 UTC](https://meta.discourse.org/t/activerecord-recordnotunique-on-idx-tag-users-ix1-how-to-fix/287078/4 "2023-11-30T14:38:36Z")

</div>

我不知道它是怎么发生的，但我可以删除重复的标签：

```plaintext
TagUser.all.where(user_id: 80, tag_id:2).last.destroy

```

对于每一个重复项（我是手动完成的）。

谢谢你，Jay！

---

<div class="post-metadata">

**Author:** ![hellekin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/hellekin/32/51636_2.png) [@hellekin](https://meta.discourse.org/u/hellekin)\
**Post date:** [2023年十一月30日 14:41 UTC](https://meta.discourse.org/t/activerecord-recordnotunique-on-idx-tag-users-ix1-how-to-fix/287078/5 "2023-11-30T14:41:48Z")

</div>

请注意， **症状** 已清除，但原因尚不清楚。

🤔 我怀疑标签上的组通知在追溯应用时，不会检查现有条目 @pfaffman

---

<div class="post-metadata">

**Author:** ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)\
**Post date:** [2023年十一月30日 15:41 UTC](https://meta.discourse.org/t/activerecord-recordnotunique-on-idx-tag-users-ix1-how-to-fix/287078/6 "2023-11-30T15:41:04Z")

</div>

> [@hellekin](#):
>
> 请注意， **症状** 已清除，但原因尚不清楚。

这些重复的 `whatever` 问题通常都是这样。

---

<div class="post-metadata">

**Author:** ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)\
**Post date:** [2023年十二月30日 15:41 UTC](https://meta.discourse.org/t/activerecord-recordnotunique-on-idx-tag-users-ix1-how-to-fix/287078/7 "2023-12-30T15:41:16Z")

</div>

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