From 5ff2dcc47c04e351ed65c9c12816bd87c517df33 Mon Sep 17 00:00:00 2001 From: writsop Date: Sat, 11 Apr 2026 00:47:24 -0500 Subject: [PATCH] fix: deduplicate ogImages by URL before upsert to prevent unique constraint violation When multiple OG images share the same URL, the parallel upsert calls race on the (originId, url) unique constraint causing PrismaClientKnownRequestError P2002. Deduplicate the array by URL using a Map before upserting, keeping the last occurrence (matching current upsert "last write wins" semantics). Fixes #287 --- apps/scan/src/services/db/resources/origin.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/apps/scan/src/services/db/resources/origin.ts b/apps/scan/src/services/db/resources/origin.ts index 69161432e..46af324a0 100644 --- a/apps/scan/src/services/db/resources/origin.ts +++ b/apps/scan/src/services/db/resources/origin.ts @@ -45,8 +45,11 @@ export const upsertOrigin = async ( const originId = upsertedOrigin.id; + // Deduplicate ogImages by URL to prevent unique constraint violations + const uniqueOgImages = [...new Map(origin.ogImages.map(img => [img.url, img])).values()]; + await Promise.all( - origin.ogImages.map(({ url, height, width, title, description }) => + uniqueOgImages.map(({ url, height, width, title, description }) => tx.ogImage.upsert({ where: { originId_url: {