-
Notifications
You must be signed in to change notification settings - Fork 904
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
Delete HasMany subentry when relation column is not nullable #4205
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love the extra tests! This gives me so much confidence. Just have one small suggestion, tests run fine after the suggested change.
Let's talk a little bit about this. Previously: we were NOT deleting entries, by default. AT ALL. Unless you specified After this PR: We have a case where we default to deleting. So let's be careful about this. Are we 100% sure people will want to delete the entries unless Can you please give me an example @pxpm ? The easiest for me to understand would be with the entities in the Demo. |
Hey @tabacitu Sure. I just recall we already talked about this. And here we are again. Sorry for that, I think this is what makes sense and what I expect to happen if STEP 1: Go to monsters and create one filling the STEP 2: Delete one of the HasMany, leave the other. Save. Check database, you have 1 item there. STEP3: Delete all remaining HasMany items. Save. Check database. You will see that now, instead of deleting the items, we set the key to Let me know, |
Co-authored-by: Cristian Tabacitu <[email protected]>
Hmmm ok... I get it now. But let's please change it so that the default action is non-destructive. Destructive actions should ONLY happen in that particular use case, where we know that's what we want. Otherwise do an update, which is safe. We shoud never ever fall back to a destructive action. So maybe we do something like this: if (!$relationColumnIsNullable && !$dbColumnDefault) {
return $removedEntries->delete();
}
return $removedEntries->update([$relationForeignKey => $dbColumnDefault]); Conditional is probably not ok, but you get what I mean. |
[ci skip] [skip ci]
@tabacitu refactored the condition. Found a way for you to easy test when there are FK restrictions in database. In PetShop, Create an invoice add one item, save. Open the invoice, delete the item, try to save (it will not change to 0 as in the previous example, but it will throw an ugly db error about FK contrains). PS: It's realistic to remove those constrains from |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a sidenote... another option for us was in that particular instance... instead of running a DELETE. To throw an error telling the developer to use |
WHY
BEFORE - What was wrong? What was happening before this PR?
reported in #4187 when column is nullable the default behaviour should be the deletion when no default is provided in database. Our conditional was wrong and that was not happening.
AFTER - What is happening after this PR?
It works as expected. I also added tests! 👍
HOW
How did you achieve that, in technical terms?
refactored the conditional
Is it a breaking change or non-breaking change?
nop
How can we test the before & after?
run test suit without this PR changes (only the tests), run with all the changes.