# PG::UniqueViolation 升级 3.1.0.beta4 期间出现问题

**URL:** <https://meta.discourse.org/t/pg-uniqueviolation-problem-during-3-1-0-beta4-upgrade/262163>\
**Category:** Self-hosting\
**Created:** [2023年四月19日 01:03 UTC](https://meta.discourse.org/t/pg-uniqueviolation-problem-during-3-1-0-beta4-upgrade/262163 "2023-04-19T01:03:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahmedeldeep](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ahmedeldeep/32/121601_2.png) [@ahmedeldeep](https://meta.discourse.org/u/ahmedeldeep)\
**Post date:** [2023年四月19日 01:03 UTC](https://meta.discourse.org/t/pg-uniqueviolation-problem-during-3-1-0-beta4-upgrade/262163/1 "2023-04-19T01:03:15Z")

</div>

升级到 3.1.0.beta4 时，我遇到了以下错误：

```plaintext
I, [2023-04-19T00:02:26.057232 #1] INFO -- : cd /var/www/discourse & su discourse -c 'LOAD_PLUGINS=0 bundle exec rake plugin:pull_compatible_all'
I, [2023-04-19T00:02:27.340128 #1] INFO -- : discourse-adplugin is already at latest compatible version
discourse-cakeday is already at latest compatible version
discourse-formatting-toolbar is already at latest compatible version
discourse-whos-online is already at latest compatible version
docker_manager is already at latest compatible version
vbulletin-bbcode is already at latest compatible version

I, [2023-04-19T00:02:27.340368 #1] INFO -- : cd /var/www/discourse & su discourse -c 'bundle exec rake db:migrate'
2023-04-19 00:02:30.080 UTC [634] discourse@discourse ERROR: duplicate key value violates unique constraint "index_users_on_username"
2023-04-19 00:02:30.080 UTC [634] discourse@discourse DETAIL: Key (username)=(xxx) already exists.
2023-04-19 00:02:30.080 UTC [634] discourse@discourse STATEMENT: UPDATE users SET password_algorithm = '$pbkdf2-sha256$i=64000,l=32$'
	WHERE id IN (
	  SELECT id FROM users
	  WHERE users.password_hash IS NOT NULL
	  AND users.password_algorithm IS NULL
	  LIMIT 5000
	)
	
rake aborted!
StandardError: An error has occurred, all later migrations canceled:

PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "index_users_on_username"
DETAIL: Key (username)=(xxx) already exists.

```

修复重复用户后，我又遇到了新的问题。

```plaintext
discourse=# REINDEX TABLE users;
ERROR: could not create unique index "index_users_on_username_lower"
DETAIL: Key (username_lower)=(mhm) is duplicated.
discourse=# REINDEX TABLE users;
ERROR: could not create unique index "index_users_on_username_lower"
DETAIL: Key (username_lower)=(ahmedhafez) is duplicated.
CONTEXT: parallel worker
discourse=# REINDEX TABLE users;
ERROR: could not create unique index "index_users_on_username_lower"
DETAIL: Key (username_lower)=(hany) is duplicated.
discourse=# REINDEX TABLE users;
ERROR: could not create unique index "index_users_on_username_lower"
DETAIL: Key (username_lower)=(eng_ali) is duplicated.
CONTEXT: parallel worker
discourse=# REINDEX TABLE users;
ERROR: could not create unique index "index_users_on_username_lower"
DETAIL: Key (username_lower)=(saad_saad) is duplicated.
discourse=# REINDEX TABLE users;
ERROR: could not create unique index "index_users_on_username_lower"
DETAIL: Key (username_lower)=(eng_ali) is duplicated.
CONTEXT: parallel worker
discourse=# REINDEX TABLE users;
ERROR: could not create unique index "index_users_on_username_lower"
DETAIL: Key (username_lower)=(mostafa11) is duplicated.
CONTEXT: parallel worker

```

---

<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年四月19日 07:10 UTC](https://meta.discourse.org/t/pg-uniqueviolation-problem-during-3-1-0-beta4-upgrade/262163/2 "2023-04-19T07:10:02Z")

</div>

这看起来像是索引损坏， ~~或者用户以不同的字母大小写创建了同名账户~~ 。

您应该能够使用以下命令启动旧容器：

./launcher start app

然后您可以尝试重新索引表，找出哪个用户有问题，修复它，然后重复此过程直到索引重建。

---

<div class="post-metadata">

**Author:** ![ahmedeldeep](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ahmedeldeep/32/121601_2.png) [@ahmedeldeep](https://meta.discourse.org/u/ahmedeldeep)\
**Post date:** [2023年四月19日 09:06 UTC](https://meta.discourse.org/t/pg-uniqueviolation-problem-during-3-1-0-beta4-upgrade/262163/3 "2023-04-19T09:06:08Z")

</div>

感谢您的回复。

是的，我能够启动/进入应用程序，并且我正在按照您的建议进行操作。

```plaintext
discourse=# REINDEX TABLE users;
ERROR: could not create unique index "index_users_on_username_lower"
DETAIL: Key (username_lower)=(mostafa11) is duplicated.

```

问题是重复项不断出现，我拥有超过 30 万用户。  
有什么方法可以检查重复项的数量或列出所有重复项吗？

对于修复，我像这样更新了 username\_lower：我在末尾附加了一个数字。

`UPDATE users SET username_lower = 'xxx' WHERE id = xxx;`

我是否还需要更新 username 列以匹配 username\_lower？有没有更快的自动修复重复项的方法？

我的网站现在已离线，因此非常感谢您的任何帮助。

---

<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年四月19日 09:16 UTC](https://meta.discourse.org/t/pg-uniqueviolation-problem-during-3-1-0-beta4-upgrade/262163/4 "2023-04-19T09:16:47Z")

</div>

> [@ahmedeldeep](#):
>
> 有什么办法可以检查有多少重复项或列出所有重复项吗？

我不确定。我认为这不应该发生。您是从哪个版本升级的？

也许您可以搜索用户名不等于 username\_lower 的用户？

> [@ahmedeldeep](#):
>
> 我的网站现在离线了，所以任何帮助都将不胜感激。

即使您启动了应用程序，它还是崩溃了吗？

> [@ahmedeldeep](#):
>
> 我还需要更新 username 列以匹配 username\_lower 吗？

不。用户名的字母大小写混合是可以的，但您不希望有两个用户名包含相同的字母。

您是否同时拥有 Joe 和 joe？如果是这种情况，我认为应该有一个查询可以找到它们，但我现在不知道如何操作。

---

<div class="post-metadata">

**Author:** ![ahmedeldeep](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ahmedeldeep/32/121601_2.png) [@ahmedeldeep](https://meta.discourse.org/u/ahmedeldeep)\
**Post date:** [2023年四月19日 17:15 UTC](https://meta.discourse.org/t/pg-uniqueviolation-problem-during-3-1-0-beta4-upgrade/262163/5 "2023-04-19T17:15:34Z")

</div>

谢谢 Jay  
我能够重建并重新上线我的网站。

首先，正如你建议的那样，解决方案是重新索引、修复并重复直到完成。我使用了以下命令来查看有多少

```plaintext
SELECT username_lower, count(*) from users GROUP by username_lower HAVING count(*) > 1;

```

它返回零。

当然，有很多重复项，我想知道有多少。所以我稍微调整了查询：

```plaintext
SELECT username_lower, count(username) from users GROUP by username_lower HAVING count(username) > 1;

```

这奏效了，并返回了大约 50 个重复项，所以我修复了它们，重新索引并成功重建。

---

<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年四月19日 17:16 UTC](https://meta.discourse.org/t/pg-uniqueviolation-problem-during-3-1-0-beta4-upgrade/262163/6 "2023-04-19T17:16:54Z")

</div>

太好了！很高兴你解决了这个问题。你的笔记对遇到同样问题的人会非常有帮助。

我编辑了你的帖子，方便其他人复制粘贴你的 SQL。

---

<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年五月19日 17:17 UTC](https://meta.discourse.org/t/pg-uniqueviolation-problem-during-3-1-0-beta4-upgrade/262163/7 "2023-05-19T17:17:43Z")

</div>

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