Skip to content

fix: handle OgImage unique constraint conflict when multiple origins share the same OG image URL#791

Open
tarai-dl wants to merge 1 commit into
Merit-Systems:mainfrom
tarai-dl:rn/fix-ogimage-unique-constraint
Open

fix: handle OgImage unique constraint conflict when multiple origins share the same OG image URL#791
tarai-dl wants to merge 1 commit into
Merit-Systems:mainfrom
tarai-dl:rn/fix-ogimage-unique-constraint

Conversation

@tarai-dl
Copy link
Copy Markdown

Summary

Closes #287

This PR fixes the database unique constraint error that occurs when registering resources from different origins that share the same OG image URL.

Problem

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

This happens when:

  1. Resource A with URL: https://subdomain.example.com/api/endpoint registers with origin: https://subdomain.example.com
  2. Resource B with URL: https://example.com/api/endpoint has origin: https://example.com
  3. Both domains share the same OG image URL (e.g., due to redirect or shared CDN)
  4. Registration fails with the unique constraint error

Solution

The fix modifies the upsertOrigin function in apps/scan/src/services/db/resources/origin.ts to handle the case where an OG image URL is already used by another origin:

  1. First checks if an OgImage with the same URL already exists for the current origin
  2. If it exists, updates it with the new metadata
  3. If it doesn't exist, tries to create a new one
  4. If creation fails due to a unique constraint on the URL (used by another origin), it logs a warning and skips creation

This allows multiple origins to share the same OG image URL without causing registration failures.

Changes

  • Modified upsertOrigin function to handle unique constraint errors gracefully
  • Added error handling for Prisma unique constraint violations (error code P2002)
  • Added warning logging when skipping OgImage creation due to URL conflicts

Testing

The fix has been tested to ensure:

  • Existing functionality remains unchanged
  • Multiple origins can now share the same OG image URL
  • Proper error handling and logging is in place

…share the same OG image URL

Previously, when two different origins (e.g., subdomain.example.com and example.com) shared the same OG image URL, the registration would fail with a unique constraint error.

This fix:
1. First checks if an OgImage with the same URL already exists for the current origin
2. If it exists, updates it with the new metadata
3. If it doesn't exist, tries to create a new one
4. If creation fails due to a unique constraint on the URL (used by another origin), it logs a warning and skips creation

This allows multiple origins to share the same OG image URL without causing registration failures.

Closes Merit-Systems#287
@vercel
Copy link
Copy Markdown
Contributor

vercel Bot commented Apr 18, 2026

@tarai-dl is attempting to deploy a commit to the Merit Systems Team on Vercel.

A member of the Team first needs to authorize it.

title,
description,
},
});
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Silent null return in Promise.all discards race condition failures when creating OgImages with duplicate (originId, url) pairs, and error handling lacks type safety

Fix on Vercel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Unable to register resource due to OgImage unique constraint conflict

1 participant