All writing

Merging Duplicate Feedback Without Losing a Vote (Postgres and Prisma)

By Bronson Dunbar ·

Every feedback board fills up with duplicates. "Dark mode", "Dark theme" and "Please add a night mode!" are the same request, and leaving them separate splits the votes three ways and hides how much people actually want it.

Merging sounds simple: move everything from one request to the other and hide the old one. In Feedbackly it turned out to have five traps. This post goes through each one, with the Prisma code that handles it.

Feedbackly's request management view: each request shows its vote count, value score, status and verified followers.

Everything on that screen (votes, followers, comments and the value score) has to survive a merge intact.

The data model

The relevant parts of the schema:

model Post {
  id           String  @id @default(cuid())
  boardId      String
  title        String
  description  String
  mergedIntoId String?
  mergedInto   Post?   @relation("MergedRequests", fields: [mergedIntoId], references: [id], onDelete: Cascade)
  votesCount   Int     @default(0)
  votes        Vote[]
  comments     Comment[]
  followers    RequestFollower[]
  // ...
}

model Vote {
  id          String @id @default(cuid())
  postId      String
  fingerprint String

  @@unique([postId, fingerprint])
}

Two decisions shape everything else:

  • A merged request isn't deleted. It gets a mergedIntoId pointing at the request it was merged into. Public lists filter on mergedIntoId: null, so it disappears from the board, but it still exists.
  • Votes are anonymous and identified by a fingerprint, with a unique constraint so each visitor can vote once per request. (I explain why in Why Feedbackly lets customers post without an account.)

Trap 1: the same person voted for both

If one visitor voted for both "Dark mode" and "Night theme", simply moving the votes over would break the unique constraint, or count that person twice. So before moving anything, find the fingerprints that already appear on the target and delete those votes from the source:

const fingerprints = new Set(target.votes.map((vote) => vote.fingerprint));
const duplicates = source.votes
  .filter((vote) => fingerprints.has(vote.fingerprint))
  .map((vote) => vote.id);

await tx.vote.deleteMany({ where: { id: { in: duplicates } } });
await tx.vote.updateMany({ where: { postId: sourceId }, data: { postId: targetId } });

The new total is target + source - duplicates: every real person still counts exactly once.

Trap 2: the cached vote count is wrong

votesCount is stored on each post so the board can sort by votes without counting rows on every page load. Cached numbers drift, and merging two drifted numbers makes things worse. So before merging, Feedbackly checks that each cached count matches the real number of vote rows, and refuses to continue if it doesn't:

if (source.votesCount !== source.votes.length || target.votesCount !== target.votes.length)
  throw new Error("Vote totals need reconciliation before these requests can be merged.");

Refusing is better than quietly writing a wrong number that then looks authoritative.

Trap 3: two merges at once

What if two tabs merge overlapping requests at the same moment, or someone votes while a merge is running? Everything happens inside one interactive transaction that starts by locking the board's posts:

return prisma.$transaction(async (tx) => {
  await tx.$queryRaw`SELECT id FROM "Post" WHERE "boardId" = ${boardId} ORDER BY id FOR UPDATE`;
  // ...all the reads and writes below
}, { timeout: 15000 });

FOR UPDATE makes any other transaction that wants to change those rows wait its turn. ORDER BY id matters too: two transactions that lock the same rows in different orders can deadlock, while a consistent order prevents that. Merges are rare, so making the occasional one wait a few milliseconds is a cheap price.

After the lock, the posts are read again with mergedIntoId: null, so a request that was merged a moment ago can't be merged a second time.

Trap 4: merging something that was itself a merge target

Say "Night theme" was merged into "Dark mode" last week, and today "Dark mode" is merged into "Appearance settings". If "Night theme" still pointed at "Dark mode", it would point at a hidden request. So every request that pointed at the source is moved along to the new target:

await tx.post.updateMany({ where: { mergedIntoId: sourceId }, data: { mergedIntoId: targetId } });
await tx.post.update({ where: { id: sourceId }, data: { mergedIntoId: targetId } });

The chain never gets longer than one step, so any old request can find the live one with a single lookup.

Trap 5: links in people's inboxes

Visitors can follow a request to get an email when its status changes, and those emails contain confirmation and unsubscribe links tied to the original request's ID. That's the main reason merged requests are kept instead of deleted: old links still resolve, and they can follow mergedIntoId to the live request. Someone who followed "Night theme" a month ago still hears when "Appearance settings" ships ("the request you follow was merged into this request, which is now completed"), and can still unsubscribe.

One subtle rule: if the same email followed both requests and unsubscribed from one of them, the merge must not turn that opt-out back into a subscription. When choosing who to email, any address that opted out of any merged request is left out, and duplicate addresses only get one email.

Comments move across with a single updateMany, so the discussion stays together.

Recalculating the score

Feedbackly can also give each request a value score from reach, impact, confidence and effort, and votes feed into it. After a merge, the target's score is recalculated with the new vote count, unless the board owner has overridden it by hand, in which case their number stands.

Takeaways

  • Mark merged records instead of deleting them if anything outside your database (emails, links, integrations) might refer to them.
  • Deduplicate on the natural key (here, postId and fingerprint) before moving child rows.
  • Check cached counters before you trust them in an operation that combines them.
  • Lock in a consistent order inside a single transaction when concurrent edits are possible.
  • Flatten pointer chains as you write them, so reads never have to walk a chain.

If your feedback is scattered across Slack threads and emails, Feedbackly collects it in one place. Duplicates included, and now mergeable.