"Column cannot be null" in 2.16.0 for OneToMany bidirectrional mapping #7236

Open
opened 2026-01-22 15:47:41 +01:00 by admin · 0 comments
Owner

Originally created by @SunMar on GitHub (Oct 26, 2023).

BC Break Report

Q A
BC Break yes
Version 2.16.0

Summary

I have a OneToMany bidirectional mapping between a parent and child entity. After upgrading to 2.16.0 I encountered the following error when persisting entities:

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'parent_id' cannot be null

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 OneToMany relationship with the child, and the child cross-references a ManyToOne back 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 uses NONE as 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_id column is the column on the child that references back to the parent and is a non-nullable column. In BasicEntityPersister->prepareUpdateData() the value for parent_id is 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 null error.

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.

Originally created by @SunMar on GitHub (Oct 26, 2023). <!-- Before reporting a BC break, please consult the upgrading document to make sure it's not an expected change: https://github.com/doctrine/orm/blob/2.9.x/UPGRADE.md --> ### BC Break Report <!-- Fill in the relevant information below to help triage your issue. --> | Q | A |------------ | ------ | BC Break | yes | Version | 2.16.0 #### Summary I have a `OneToMany` bidirectional mapping between a parent and child entity. After upgrading to 2.16.0 I encountered the following error when persisting entities: ``` PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'parent_id' cannot be null ``` #### 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 `OneToMany` relationship with the child, and the child cross-references a `ManyToOne` back 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 uses `NONE` as 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_id` column is the column on the child that references back to the parent and is a non-nullable column. In `BasicEntityPersister->prepareUpdateData()` the value for `parent_id` is set to null, because the parent is scheduled for insertion as well: https://github.com/doctrine/orm/blob/17500f56eaa930f5cd14d765bc2cd851c7d37cc0/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 null` error. 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.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#7236