oneToOne unidirectional association update fails #5027

Open
opened 2026-01-22 14:56:55 +01:00 by admin · 2 comments
Owner

Originally created by @Tomsgu on GitHub (Feb 28, 2016).

Hi everyone,

I have an unidirectional oneToOne relationship between a Person and User. I am trying to execute method mergePeople.

public function mergePeople($personA, $personB)
{
    //...
    $personB->setUser($personA->getUser());
    $personA->setUser(NULL);
    $this->em->remove($personA);
    $this->em->flush();
}

But this code throws an exception:

An exception occurred while executing 'UPDATE person SET last_name = ?, user_id = ? WHERE id = ?' with params ["Sanchez", 298, 47]:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '298' for key 'UNIQ_6F549197A76ED395' 

I am wondering why I get this error. Is it some kind of bug(in my code or somewhere else) or doctrine doesn't handle this situation normally(should firstly flush setUser(NULL) change and then other changes)?

doctrine/orm 2.4.8
doctrine/dbal 2.4.5
Symfony doctrine2 bundle 1.6.2

Originally created by @Tomsgu on GitHub (Feb 28, 2016). Hi everyone, I have an unidirectional oneToOne relationship between a Person and User. I am trying to execute method mergePeople. ``` php public function mergePeople($personA, $personB) { //... $personB->setUser($personA->getUser()); $personA->setUser(NULL); $this->em->remove($personA); $this->em->flush(); } ``` But this code throws an exception: ``` An exception occurred while executing 'UPDATE person SET last_name = ?, user_id = ? WHERE id = ?' with params ["Sanchez", 298, 47]: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '298' for key 'UNIQ_6F549197A76ED395' ``` I am wondering why I get this error. Is it some kind of bug(in my code or somewhere else) or doctrine doesn't handle this situation normally(should firstly flush `setUser(NULL)` change and then other changes)? `doctrine/orm 2.4.8` `doctrine/dbal 2.4.5` `Symfony doctrine2 bundle 1.6.2`
Author
Owner

@baptistedonaux commented on GitHub (Mar 1, 2016):

Same fail. Cascade updates are also impacted. +1

@baptistedonaux commented on GitHub (Mar 1, 2016): Same fail. Cascade updates are also impacted. +1
Author
Owner

@akomm commented on GitHub (Apr 21, 2017):

Same here.

@akomm commented on GitHub (Apr 21, 2017): Same here.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5027