Description
When attempting to register a new resource, the registration fails with a database unique constraint error:
Unique constraint failed on the fields: (url)
Invalid prisma.resourceOrigin.upsert() invocation
Steps to Reproduce
- Register Resource A with URL:
https://subdomain.example.com/api/endpoint
- This successfully registers with origin:
https://subdomain.example.com
- During registration, the system scrapes OG metadata and creates an OgImage record
- Attempt to register Resource B with URL:
https://example.com/api/endpoint
- This has a different origin:
https://example.com
- Both domains share the same OG image URL (e.g., due to redirect or shared CDN)
- Registration fails with the unique constraint error
Root Cause
The issue is in /apps/scan/src/services/db/resources/origin.ts:64-74 where the create operation for OgImages does not handle the case where an OG image URL is already used by another
origin.
create: {
origin: origin.origin,
// ...
ogImages: {
create: origin.ogImages.map(({ url, height, width, title, description }) => ({
url, // ❌ Fails if URL already exists globally
height,
width,
title,
description,
})),
},
}
The OgImage table has a global unique constraint on the url field (schema.prisma:207), but multiple origins can legitimately share the same OG image URL.
Note that the update branch (lines 36-56) correctly uses upsert for OgImages, but the create branch does not.
Expected Behavior
- Multiple origins should be able to share the same OG image URL
- Resource registration should succeed even when OG images are shared between origins
Suggested Fix
Change the create operation to use connectOrCreate instead:
create: {
origin: origin.origin,
title: origin.title,
description: origin.description,
favicon: origin.favicon,
ogImages: {
connectOrCreate: origin.ogImages.map(
({ url, height, width, title, description }) => ({
where: { url },
create: {
url,
height,
width,
title,
description,
},
})
),
},
}
This would allow the same OG image to be connected to multiple origins, which is the correct behavior since the schema already has a composite unique constraint @@unique([originId, url]) that
permits this relationship.
Environment
- Affected file: apps/scan/src/services/db/resources/origin.ts
- Database schema: apps/scan/prisma/schema.prisma (OgImage model, line 204-216)
Description
When attempting to register a new resource, the registration fails with a database unique constraint error:
Unique constraint failed on the fields: (url)
Invalid prisma.resourceOrigin.upsert() invocation
Steps to Reproduce
https://subdomain.example.com/api/endpointhttps://subdomain.example.comhttps://example.com/api/endpointhttps://example.comRoot Cause
The issue is in
/apps/scan/src/services/db/resources/origin.ts:64-74where thecreateoperation for OgImages does not handle the case where an OG image URL is already used by anotherorigin.
The OgImage table has a global unique constraint on the url field (schema.prisma:207), but multiple origins can legitimately share the same OG image URL.
Note that the update branch (lines 36-56) correctly uses upsert for OgImages, but the create branch does not.
Expected Behavior
Suggested Fix
Change the create operation to use connectOrCreate instead:
This would allow the same OG image to be connected to multiple origins, which is the correct behavior since the schema already has a composite unique constraint @@unique([originId, url]) that
permits this relationship.
Environment