Isn't weird to get unique constraint
violation when using updateOrCreate
method?
#53363
Unanswered
foremtehan
asked this question in
General
Replies: 2 comments 3 replies
-
You could try |
Beta Was this translation helpful? Give feedback.
2 replies
-
Maybe, it's better to use a single, atomic operation than one public function handle()
{
Model::query()->upsert(['video_id' => $videoId, 'status' => $status], ['video_id'], ['status']);
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm using
updateOrCreate
in some job workers like this:I've set up 3 queue workers in supervisor.
However, sometimes (not always), I get this error:
Isn't that strange? Shouldn't
updateOrCreate
check if the record exists before trying to create it? My guess is that the 3 processes might be attempting to create the same record simultaneously. How can I prevent this? Do I need to handle it myself, or does laravel have a built-in solution for this kind of issue?Beta Was this translation helpful? Give feedback.
All reactions