# 合并具有重复投票的用户时出错

**URL:** https://meta.discourse.org/t/error-merging-users-with-duplicate-poll-votes/154711
**Category:** Bug
**Tags:** merge-user
**Created:** [2020年六月12日 22:17 UTC](https://meta.discourse.org/t/error-merging-users-with-duplicate-poll-votes/154711 "2020-06-12T22:17:12Z")
**Posts on this page:** 1
**Showing post:** 10

<div class="post-metadata">

### Author: ![nat](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nat/32/235063_2.png) [@nat](https://meta.discourse.org/u/nat)
#### Post date: [2023年六月14日 11:16 UTC](https://meta.discourse.org/t/error-merging-users-with-duplicate-poll-votes/154711/10 "2023-06-14T11:16:27Z")

</div>

这是一个有趣的 bug。

由于投票未正确合并，当我们合并 Lancelot（ID 383）-\> Galahad（ID 120）时，Lancelot 实际上仍然存在，但没有主电子邮件 - `/admin/users/383/lancelot` 会因为匿名化检查而导致 http 500 错误。

我们正在修复这个问题。在这种重复的情况下，我们倾向于保留 Galahad（目标用户）的投票。

> <https://github.com/discourse/discourse/pull/22105>
>
> This is somewhat of an uncommon case where if a plugin fails to merge a user, th…e merge would fail but the \`user\_email\` of the \`source\_user\` would have been transferred to the \`target\_user\`. This leaves the \`source\_user\` without an email, and this bug would occur when admins try to access \`/admin/users/\<id\>/\<username\>\`.
> 
> This allows the admin to continue dealing with the user.
> 
> https://meta.discourse.org/t/error-merging-users-with-duplicate-poll-votes/154711
> 
> A consideration here would be to use a transaction (probably very costly), or reorder the order of events in \`UserMerger.merge!\`

> <https://github.com/discourse/discourse/pull/22107>
>
> Currently when merging users, polls may error out if the source and target users… have both voted on the same poll before. 😢 
> 
> There is currently no constraint on the \`poll\_votes\` table either to support this. Ideally a composite primary key can be used \`(poll\_id, user\_id)\`, but alas there is no support yet, which is probably why it wasn't created in the first place.
> 
> This fix ensures that merging is successful by only keeping the target poll votes if duplicates exist.
> 
> This fix also runs a migration on older poll votes where failed merges would have caused a single user to have voted twice on a single poll. e.g. this weird edge case
> 
> \<img width="450" alt="Screenshot 2023-06-14 at 7 12 06 PM" src="https://github.com/discourse/discourse/assets/1555215/19d9ed06-2206-45f5-826f-e1c565f01bab"\>
> 
> Related: https://github.com/discourse/discourse/pull/22105

---

_[View the full topic](https://meta.discourse.org/t/error-merging-users-with-duplicate-poll-votes/154711)._
