-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
[10.x] Enhancing updateOrCreate()
to Use firstOrCreate()
#48160
Conversation
firstOrCreate()
for updateOrCreate()
updateOrCreate()
to Use firstOrCreate()
c3c7c08
to
ac5261a
Compare
ac5261a
to
63524ba
Compare
@taylorotwell With reference to PR #47973, the only method that was internally changed to use
For this reason, in this PR, I've only made the adjustment to Should we aim to establish the dependency chain |
@mpyw Oh, nice catch. I forgot to update the relation methods. I'll send a separate PR to address that. I think we better attempt to update all |
…hind the scenes Related to laravel#48160, laravel#48192
I'm trying to generate a simple reproduction case, but this change has broken some code for us.
This results in an error on the creation of $connection, with Simple repro example is proving tough because both our Social and Connection models are using https://github.com/tighten/parental, but I wanted to note this in case anyone else is encountering the same. |
@ceejayoz from what I can see, I don't see why the I don't see why Both Connection and Social are part of the same hierarchy? I mean, are these models pointing to the same table? If so, does Connection also use the soft-deletes trait? Can you create an issue for it referencing this PR? |
@tonysm The call results in a violation of the not-null constraint on my I've drilled down into internals a bit with dumps of the
Connection and Social are different models, each with same-table-inheritance submodels (using Parental), i.e. there's a It may be something peculiar to Parental; I'll try and get a minimum example repo put together. |
…#48531) * Avoid using createOrFirst inside other *OrCreate methods * Revert "[10.x] Make the `updateOrCreate` methods in relations use `firstOrCreate` behind the scenes (#48213)" This reverts commit e1ae507. * Revert "Enhancing `updateOrCreate()` to Use `firstOrCreate()` (#48160)" This reverts commit 80e0262. * Fix mocked firstOrCreate tests
@ceejayoz we reverted from using the |
Summary
This PR proposes a modification to the
updateOrCreate()
method in line with recent changes brought by the introduction of thecreateOrFirst()
method.Details
createOrFirst
method to Eloquent #47973createOrFirst
on transactions #48144In the previous PR by @tonysm, the
firstOrCreate()
method was enhanced to leveragecreateOrFirst()
, making it more race-condition resistant, especially in highly concurrent environments. This was achieved by attempting to create a record first and, if encountering a unique constraint violation, falling back to find the matching record. This robust approach, however, was not mirrored in theupdateOrCreate()
method.To address this and bring consistency:
By doing this, the
updateOrCreate()
method will now utilize the enhancedfirstOrCreate()
, making it more resilient against potential race conditions and ensuring that both methods share a unified and robust approach in concurrent situations.