Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

syncMedia causing Integrity constraint violation: 1062 Duplicate entry #311

Open
dbateson opened this issue Mar 2, 2023 · 3 comments
Open
Labels

Comments

@dbateson
Copy link

dbateson commented Mar 2, 2023

Im using syncMedia to replace a media attachment, however sometimes its causing a Integrity constraint violation: 1062 Duplicate entry error.

Code I'm using is:

if ($request->has('main_image')) { $product->syncMedia($request->input('main_image')['original']['id'], 'main_image'); }

@frasmage
Copy link
Collaborator

frasmage commented Mar 4, 2023

syncMedia performs two steps:

  1. detaches all media for a given tag(s)
  2. attaches the provided media identifier to the given tag(s)

My first guess would be that if you have multiple simultaneous requests modifying the same media and/or tags, you could potentially get a race condition where request A detaches the media, then request B attaches the media, then request A attempts to attach the media to the same tag. The most common approach to address this kind of problem is to wrap any such requests in a database transaction, which will hold write locks to any rows modified until the transaction is committed, preventing other requests from attempting to modify the same rows.

@dbateson
Copy link
Author

dbateson commented Mar 6, 2023

Thanks for the reply.

There definitely is not multiple requests being made to modify the record. There is only one admin.

@sgtcoder
Copy link

I have the same issue even with just uploadFile. I thought this would be fixed by now, been having this issue for years.

onDuplicateIncrement

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants