mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
Unexpected INSERT when replacing collection of B with another one after persisting A. #6460
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @danydev on GitHub (May 9, 2020).
Bug Report
Summary
In our project we encountered an error about breaking a database constraint. That happened because Doctrine wrongly decided to do an INSERT that should have not happened.
Specifically we had two classes: ClassA, ClassB. ClassA has a property 'classesB' that is a bidirectional OneToMany to ClassB with cascade persist and orphanRemoval.
Current behaviour
Doctrine produced an unexpected INSERT that should not have occurred (specifically the INSERT for the object of ClassB but that really was replaced in the collection, and thus should not be persisted at all in the database).
I understand that what probably happens is that the persist schedules the object in its inserts because of the cascade persist, but still my expectation is that such object would be unscheduled given later has been removed from the collection. Let's say that 'orphanRemoval' would normally delete it from the DB, and in this case I expect is that it should cause the unschedule from the UoW.
How to reproduce
Functional test in this PR
https://github.com/doctrine/orm/pull/8134/files
Expected behaviour
The INSERT for the object that has been replaced in the collection should not occur.
Workaround
If we move the persist for object of ClassA after replacing the collection (i.e. after step 5), then it works as expected, and only the correct INSERT is performed.
So let's say that you can always re-arrange your code to avoid the bug by setting the collection only once before doing the persist, but still I think it's a legit bug.
@mougrim commented on GitHub (Jan 25, 2021):
Have the same issue, also reproduced with OneToOne relations.