-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
Save succeeds despite associated record failing to save #32939
Comments
@gwincr11 Thanks for the pointer! Unfortunately, the behaviour is the same with or without |
Here's a reproduction. Saving a model instance succeeds in spite of it specifying |
Oh rightI I always forget that AR doesn't enforce this at the database level. I think you can add unique true to the migration column that will enforce it at the db level but I am not entirely sure how it handles this situation you are showing. 🤔 |
@gwincr11 In this case the db constraint would not get hit, which is why I omitted it. AR detects the duplication just before inserting the record, so the db is never in an invalid state. That said, in practice the constraint should definitely be there, to guard against race conditions. |
@utilum The unexpected part, for me, is that
|
I do see your point. Also the edginess of the case, as each of the objects involved is valid in and of itself. Would you have |
Yes, that sounds like the best solution to me. |
Added a fix here #32952 |
Fixed by #32796. |
Steps to reproduce
Models:
Code:
Executable test
Expected behavior
The children are invalid, so
save
should returnfalse
and no changes should be made to the database.Actual behavior
It returns
true
after saving the parent and one of the children:Since there are no uniqueness problems between each child individually and what's already in the database, validation of the parent passes. Each child is revalidated before being saved, so when the second child is saved, the duplication is detected and it is not inserted into the database. For some reason, this does not roll back the transaction.
System configuration
Rails version: 5.2.0
Ruby version: 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin17]
(See also #32940)
The text was updated successfully, but these errors were encountered: