mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
Doctrine tries to delete entities in incorrect order #6907
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 @jusephe on GitHub (Jan 13, 2022).
Bug Report
Summary
In some cases, Doctrine tries to delete entities in incorrect order and it fails on foreign keys exception. It also happens in 2.10.4
Current behavior
I have 6 entities with circular reference, and I tried to delete 2 of them. But
UnitOfWork::commit()fails because ofForeignKeyConstraintViolationException.The reason is that it first tries to delete the entity set as a foreign key in the second entity, which hasn't been deleted yet.
I investigated it quite a bit and the order of these operations is determined by
$commitOrderinUnitOfWork::commit(). It is calculated from all 6 entities, but for the delete itself, the order of those 2 entities is not correct within those 6 entities. It should be sufficient to compute the order for deletions only with those 2 entities set to be deleted. I prepared a test with described behavior and a possible fix. See linked pull request.How to reproduce
Run the test in a linked pull request with a DB platform supporting foreign key constraints. (i.e., not SQLite)
Expected behavior
UnitOfWork::commit()should delete entities. I prepared a possible fix in the linked pull request. I created a new method calledgetCommitOrderForDeletions()for computing the correct order of entities set to be deleted. This function is similar togetCommitOrder(), but it operates only with entities set to be deleted.