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

升级到 3.1.0.beta4 时,我遇到了以下错误:

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.

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

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
1 个赞

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

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

./launcher start app

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

1 个赞

感谢您的回复。

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

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?有没有更快的自动修复重复项的方法?

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

1 个赞

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

也许您可以搜索用户名不等于 username_lower 的用户?

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

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

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

2 个赞

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

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

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

它返回零。

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

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

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

1 个赞

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

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

1 个赞

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