-
Notifications
You must be signed in to change notification settings - Fork 11.2k
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] HotFix: throw captured UniqueConstraintViolationException
if there are no matching records on SELECT
retry
#48234
[10.x] HotFix: throw captured UniqueConstraintViolationException
if there are no matching records on SELECT
retry
#48234
Conversation
Looks like a good solution |
UniqueConstraintViolationException
UniqueConstraintViolationException
if there are no matching records on SELECT
retry
@taylorotwell @driesvints @tonysm Please check it out 😃 |
UniqueConstraintViolationException
if there are no matching records on SELECT
retryUniqueConstraintViolationException
if there are no matching records on SELECT
retry
Failing tests look to be irrelevant. Ready to merge. |
@mpyw if it got it right, this isn't "fixing" the problem, right? It's more like letting the error throw so the developer knows about the edge case, which I guess it makes sense. I didn't think about this scenario as I was working on this feature. 🤔 |
And the previous implementation of |
@tonysm Thank you for your comment! Firstly, it's important to clarify that a While you mentioned that this change seems to be just aiding the developer's debug process, it is crucial in the context of a framework providing functionality to web applications. The correct behavior here involves the code either succeeding or throwing a Furthermore, it is worth noting that the In conclusion, while it may appear that this change merely aids in debugging, it is a critical adjustment that prevents unintended behavior and enables developers to handle unique constraint violations more appropriately in various scenarios. |
@taylorotwell Thanks for merging! |
@tonysm I think I might understand a bit of what you're saying. Are you suggesting that, in the case of two or more independent unique keys (not composite unique keys), if one of them causes a unique constraint error, we should use the attribute that caused the error as a key to retrieve the existing record? Indeed, there might be useful cases for this, but attempting to address this would make the operation extremely complex and would likely require changing the method signature to accept groups of |
@mpyw I get it that the unique violation can be used programatically. I'm trying to think if we better switch the I think this would signal better that the record couldn't be found using the given attributes. But this wouldn't solve the issue in |
@tonysm Using Given this, I believe it's more appropriate in this context to rethrow the |
@tonysm I don't have any objections to the changes that @mpyw has made in this PR. The flow here is:
You argue that since the final operation is a 'SELECT', the appropriate exception to throw would be ModelNotFoundException. I understand that perspective. However, this behavior contradicts what a developer might expect. They would assume that the retrieval is based on $attributes failing the unique constraint, while in reality, it's $values that's causing the conflict. Thus, the root cause of the SELECT resulting in NULL is a Unique Key violation. So, shouldn't developers be dealing with UniqueViolationException instead? |
I'm not convinced that rethrowing the But, as I said, the issue with the However, it was merged, so it's okay. |
This is exactly the situation we encountered when our test suite failed updating to 10.21.0. We were getting type errors because I think throwing the |
createOrFirst
method to Eloquent #47973createOrFirst
on transactions #48144updateOrCreate()
to UsefirstOrCreate()
#48160createOrFirst()
#48161firstOrCreate
methods in relations usecreateOrFirst
behind the scenes #48192updateOrCreate
methods in relations usefirstOrCreate
behind the scenes #48213createOrFirst
does not return null at type level #48214There is an issue with
createOrFirst
up to versionv10.21.0
. This causes the following problems whencreateOrFirst()
returns NULL.firstOrCreate()
also inadvertently returns NULL.updateOrCreate()
causes an error because it tries to access the->wasRecentlyCreated
property on NULL.The latter is particularly critical, so if possible, we would like you to release it as a hot fix in version
v10.21.1
. Please consider it.