-
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] Ensuring Primary Reference on Retry in createOrFirst()
#48161
[10.x] Ensuring Primary Reference on Retry in createOrFirst()
#48161
Conversation
We need to fix unit tests with Mockery. Okay |
Why did you only update one of the |
Oops, sorry. I've just noticed that there are more |
1f0e298
to
70fde6f
Compare
@mpyw doesn't Eloquent automatically switches to the write connection after a write attempt ( The docs say that the
|
@tonysm I am aware of this feature, but I believe the read connection actually switches to the primary only after a successful write attempt. framework/src/Illuminate/Database/Connection.php Lines 560 to 613 in 3365194
According to this, |
Summary
This PR refines the
createOrFirst()
method to guarantee referencing the primary database when re-attempting record retrieval after aUniqueConstraintViolationException
.Details:
createOrFirst
method to Eloquent #47973createOrFirst
on transactions #48144The existing
createOrFirst()
method, when facing a unique constraint violation, retries by fetching the matching record. However, in environments with replication, there's no assurance that the newly created record is immediately available on replicas due to possible replication lags.To address this:
With this change, in case of a retry, the method explicitly references the primary database using
useWritePdo()
, ensuring immediate and accurate record retrieval without being affected by replication delays.