Tag Merging Unique Constraint Crash & Metadata Erasure
Author: codeCraft-RitikCreated Sep 28, 2026Updated Sep 28, 2026
Labelsbug
Overview
In mergeTags.ts, when a user attempts to merge one or more tags into an existing tag name (e.g., merging tag "frontend" into existing tag "development"), the transaction throws a database unique constraint violation and aborts with an HTTP 500 error. Furthermore, if the target tag is included in tagIds, its custom archival configurations are erased.
Impact
- Crash on Primary Use Case: The primary real-world use case for tag merging is consolidating tags into an already existing tag. Because Prisma enforces
@@unique([name, ownerId]), callingtx.tag.create({ data: { name: newTagName, ownerId: userId } })crashes whenevernewTagNamealready exists for that user. - Silent Loss of Archival Preferences: If the target tag was in
tagIds,deleteManydestroys the existing tag record along with its custom settings (archiveAsScreenshot,archiveAsMonolith,archiveAsPDF,archiveAsReadable,archiveAsWaybackMachine,aiTag), re-creating it with blank defaults.
Affected Files & Locations
- File:
apps/web/lib/api/controllers/tags/mergeTags.ts - Lines: 44–76
// apps/web/lib/api/controllers/tags/mergeTags.ts
44: const { newTag } = await prisma.$transaction(async (tx) => {
45: await tx.tag.deleteMany({
46: where: {
47: ownerId: userId,
48: id: {
49: in: tagIds,
50: },
51: },
52: });
53:
54: const newTag = await tx.tag.create({ // ❌ CRASH: Throws P2002 if newTagName already exists!
55: data: {
56: name: newTagName,
57: ownerId: userId,
58: links: {
59: connect: affectedLinks.map((id) => ({ id })),
60: },
61: },
62: });Steps to Reproduce
- Create Tag A:
"react"(ID: 10). - Create Tag B:
"reactjs"(ID: 11). - Assign Tag B to any link.
- Attempt to merge
"reactjs"into"react"via the Tag Merge API:{ "tagIds": [11], "newTagName": "react" } - Result: The database rejects the query:
Unique constraint failed on the fields: (name, ownerId)The API returns HTTP 500, and the merge fails completely.
Proposed Fix
Implement tag existence resolution: if newTagName already exists for the user, re-link all affected links to the existing tag and only delete the source tags (preserving the destination tag and its settings). If it does not exist, create the new tag.
--- a/apps/web/lib/api/controllers/tags/mergeTags.ts
+++ b/apps/web/lib/api/controllers/tags/mergeTags.ts
@@ -42,17 +42,40 @@ export default async function mergeTags(
).map((link) => link.id);
const { newTag } = await prisma.$transaction(async (tx) => {
+ // 1. Check if a tag with newTagName already exists for this user
+ const existingTargetTag = await tx.tag.findUnique({
+ where: {
+ name_ownerId: {
+ name: newTagName,
+ ownerId: userId,
+ },
+ },
+ });
+
+ // 2. Only delete tags that are NOT the destination tag
+ const tagsToDelete = existingTargetTag
+ ? tagIds.filter((id) => id !== existingTargetTag.id)
+ : tagIds;
+
await tx.tag.deleteMany({
where: {
ownerId: userId,
id: {
- in: tagIds,
+ in: tagsToDelete,
},
},
});
- const newTag = await tx.tag.create({
- data: {
+ // 3. Connect affected links to the existing tag or create a new one
+ const newTag = existingTargetTag
+ ? await tx.tag.update({
+ where: { id: existingTargetTag.id },
+ data: {
+ links: {
+ connect: affectedLinks.map((id) => ({ id })),
+ },
+ },
+ })
+ : await tx.tag.create({
+ data: {
name: newTagName,
ownerId: userId,
links: {
connect: affectedLinks.map((id) => ({ id })),
},
},
});Source: linkwarden/linkwarden