mirror of
https://github.com/doctrine/orm.git
synced 2026-03-24 06:52:09 +01:00
"Column cannot be null" in 2.16.0 for OneToMany bidirectrional mapping #7236
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 @SunMar on GitHub (Oct 26, 2023).
BC Break Report
Summary
I have a
OneToManybidirectional mapping between a parent and child entity. After upgrading to 2.16.0 I encountered the following error when persisting entities:Previous behavior
When I downgrade to 2.15.5, the entity and its relations are inserted successfully.
Current behavior
The above error message is thrown when trying to persist the entity.
How to reproduce
After some digging I found out that the problem is that the child is being inserted before the parent.
The parent entity has a
OneToManyrelationship with the child, and the child cross-references aManyToOneback to the parent (like in https://www.doctrine-project.org/projects/doctrine-orm/en/2.16/reference/association-mapping.html#one-to-many-bidirectional). The primary key for the parent usesNONEas its generator strategy. It's a UUID generated on the application, which is set in both the parent and the child entity before persisting.The
parent_idcolumn is the column on the child that references back to the parent and is a non-nullable column. InBasicEntityPersister->prepareUpdateData()the value forparent_idis set to null, because the parent is scheduled for insertion as well:17500f56ea/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php (L701-L709)It then schedules an update, but it never gets there because when the child is inserted first, it causes the
Column 'parent_id' cannot be nullerror.Both are persisted using a single call to
$entityManager->persist($parentEntity). I couldn't find a way to change the order to make sure the parent gets inserted first.