mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
Removing one side of BiDi 1-1 relation throws #7409
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 @duzenko on GitHub (Aug 22, 2024).
Bug Report
Summary
I have added an
inverseByfield to an existing entity class so as to use it in the BiDi 1-1 fashion. The DB structure did not change. The setup is similar to the example given at https://www.doctrine-project.org/projects/doctrine-orm/en/2.19/reference/association-mapping.html#one-to-one-bidirectional.After this, under certain scenarios, deleting an entity throws an Exception.
Specifically, the entities are loaded via a DQL query:
This returns a nested object structure as expected:

After removing the
$confirmationentity the structure becomes instable:When I try commit the transaction after this, I get an exception:
Which means that Doctrine is trying to persist the
confirmationentity back to the DB.This is the opposite to what the code did before successfully (before the additing of the
inversedBy confirmationfield in theEvententity.Current behavior
Throws
How to reproduce
Delete the owner side of a BiDi 1-1 relation and commit.
Expected behavior
Delete successful. The inversed side changes to null.
@duzenko commented on GitHub (Aug 22, 2024):
Relevant field definitions:
This was added (along with
inversedByabove) before the bug manifested:@duzenko commented on GitHub (Aug 22, 2024):
It seems that changing
SELECT e, ec FROMtoSELECT ec FROMhelps in this particular case as it changes the type ofeventfield from Entity to Proxy. However in general this kind of breakage should not occur at all.@duzenko commented on GitHub (Sep 6, 2024):
It would be great to get some feedback regarding this
The bug also manifests with proxy objects after they get initialized
@greg0ire commented on GitHub (Sep 6, 2024):
I wouldn't be so sure.
Looking at the stack trace, we can see that we are here:
cfc0655a1c/src/UnitOfWork.php (L353-L369)So, directly inside
commit(). I think you were mislead into thinking it has to do with persistence because of the error message.I think you should debug further as to why this entity is considered new, but before that, have you validated your schema? I find it suspicious that such a big bug with what appears to be a pretty common situation happens at all.
You could also try reproducing it with a test.
@duzenko commented on GitHub (Sep 6, 2024):
@greg0ire Do you have a test already that deletes an entity on the owner side of a BiDi relation? I'd think no, which is why it was not noticed
And I don't think that deleting a BiDi owner (with a second flush after that) is pretty common
There is a chance that I'm doing something wrong, but I'd like to hear from you first
Note that
commitis not necessary. It can be a secondflushas well.@duzenko commented on GitHub (Sep 6, 2024):
@greg0ire This reproduced the error reliably for me
Output
@greg0ire commented on GitHub (Sep 6, 2024):
Is it a minimal reproducer? For instance, is the
JoinColumnattribute necessary to reproduce the issue? Also, when I wrote "You could also try reproducing it with a test." I meant a PHPUnit test that you would add to thedoctrine/ormtest suite.@duzenko commented on GitHub (Sep 7, 2024):
How would I know if it is necessary? I took this code from your official docs at https://www.doctrine-project.org/projects/doctrine-orm/en/2.19/reference/association-mapping.html.
Sorry, does it mean the a PHP file that you can run from CLI or IDE directly is not acepted as reproduction case? I'd think that you could convert it to a PHPUnit test in less than 5 minutes if you were interested in this bug, without the lengthy procedure of me creating a PR just for it to chill in the waiting list with another 298 PRs you already have.
My originial post had less code for which you advised to "debug further" on my side implying that I'm not using Doctrine right.
I don't think I should be getting any more of these "try harder" responses at this point
@greg0ire commented on GitHub (Sep 7, 2024):
Let me focus on the 298 PRs first (more actually if you can't other repos), and I will get back to you.