mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
Persist problem with "Entity has identity through a foreign entity" #5725
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 @va5ja on GitHub (Oct 3, 2017).
Originally assigned to: @Ocramius on GitHub.
Hi guys,
I'm having a hard time understanding why Doctrine (version 2.5.3) won't handle the following schemas as expected. I made two example entities
TestandTestTranslated. In the first example theTestTranslatedtable has a dedicated primary key and in the second example theTestTranslatedentity has a compound primary key build out of two foreign keys.Example1:
In this case everything works as expected. Running the code:
I get MySQL entries in both
TestandTestTranslatedentities with one flush. So all good.Example2:
In this case I get an error:
Entity of type ...Bundle\Entity\TestTranslation has identity through a foreign entity ...Bundle\Entity\Test, however this entity has no identity itself. You have to call EntityManager#persist() on the related entity and make sure that an identifier was generated before trying to persist '...Bundle\Entity\TestTranslation'. In case of Post Insert ID Generation (such as MySQL Auto-Increment or PostgreSQL SERIAL) this means you have to call EntityManager#flush() between both persist operations.What I can do is run the code in the following way:
but I don't understand why in this case Doctrine isn't that smart to deal with insert sequence. Meaning that it would first save
Testentity and thenTestTranslated. It works as expected with Example1 but not with Example2 although there's almost no difference in table schema. The only difference is the compound primary key.I personally don't see any reason why Doctrine wouldn't act the same. Is there a reason behind this behavior or is it just expected that every table has a dedicated primary key? I prefer having a compound primary key.
@lcobucci commented on GitHub (Nov 26, 2017):
This seems to be related to https://github.com/doctrine/doctrine2/pull/6701 (and duplicates #6531 and #6043).
@va5ja you can use a pre-generated value as ID in order to solve this for now (UUID, for example).
@Ocramius commented on GitHub (Feb 27, 2018):
@va5ja please verify if this is actually a duplicate - we merged all related changes and will release
2.6.1with them.@va5ja commented on GitHub (Mar 1, 2018):
I don't get the above error anymore. Both examples work fine so obviously the problem has been fixed and this issue is a duplicate.
@Ocramius commented on GitHub (Mar 1, 2018):
@va5ja thanks!