doctrine / orm

Doctrine Object Relational Mapper (ORM)
https://www.doctrine-project.org/projects/orm.html
MIT License
9.93k stars 2.51k forks source link

oneToOne unidirectional association update fails #5685

Open Tomsgu opened 8 years ago

Tomsgu commented 8 years ago

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

baptistedonaux commented 8 years ago

Same fail. Cascade updates are also impacted. +1

akomm commented 7 years ago

Same here.