[DDC-1666] orphanRemoval does not work with oneToOne: Duplicate entry Error Created: 23/Feb/12 Updated: 14/Mar/13 Resolved: 14/Mar/13 |
|
| Status: | Resolved |
| Project: | Doctrine 2 - ORM |
| Component/s: | ORM |
| Affects Version/s: | 2.2 |
| Fix Version/s: | 2.3.3 |
| Security Level: | All |
| Type: | Bug | Priority: | Major |
| Reporter: | Mario Knippfeld | Assignee: | Benjamin Eberlei |
| Resolution: | Fixed | Votes: | 1 |
| Labels: | None | ||
| Description |
|
orphanRemoval does not work with oneToOne. Mysql Error /*
Error:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1' for key 'UNIQ_9D8DDB05579B502F'
*/
Models and YAML Mapping
/*
Contact:
type: entity
table: contact
fields:
_id:
id: true
type: integer
unsigned: true
nullable: false
generator:
strategy: AUTO
column: id
oneToOne:
_standingData:
targetEntity: StandingData
mappedBy: _contact
cascade: ["persist", "merge", "remove"]
orphanRemoval: true
*/
class Contact
{
private $_id;
private $_standingData;
public function newStandingData(StandingData $sd)
{
$this->_standingData = $sd;
$sd->setContact($this);
}
}
/*
StandingData:
type: entity
table: standing_data
fields:
_id:
id: true
type: integer
unsigned: true
nullable: false
generator:
strategy: AUTO
column: id
oneToOne:
_contact:
targetEntity: Contact
inversedBy: _standingData
joinColumns:
contact_id:
referencedColumnName: id
onDelete: cascade
*/
class StandingData
{
private $_id;
private $_contact;
public function setContact(Contact $c)
{
$this->_contact = $c;
}
}
Script // Create new Contact $contact = new Contact(); $contact->newStandingData(new \StandingData()); $em->persist($contact); $em->flush(); // Try to change StandingData $contact->newStandingData(new \StandingData()); $em->flush(); |
| Comments |
| Comment by Benjamin Eberlei [ 14/Mar/12 ] |
|
This is a necessary restriction for the internals of Doctrine to always work correctly. |
| Comment by Albert Casademont [ 12/Apr/12 ] |
|
+1 on this one. So what's the point of orphanRemoval in OneToOne if it can never be done? Maybe onetoones should not create a unique index but a normal one. If it is really a won't fix, then the docs should reflect that. |
| Comment by Albert Casademont [ 12/Apr/12 ] |
|
BTW, as a workaround, i suggest converting this OneToOne case (where the orphanRemoval is in the inverse side) into a ManyToOne so a normal index will be created, not a unique one. Then the new row is inserted fine and the old one is deleted afterwards. |
| Comment by Mario Knippfeld [ 04/May/12 ] |
|
+1 |
| Comment by Matthieu Napoli [ 11/Feb/13 ] |
|
+1 same for me If the example of the docs isn't supposed to work (i.e. orphanRemoval with oneToOne isn't supported), then the docs should be updated. I can do a PR, but I need you to confirm it's really the case Benjamin Eberlei. |
| Comment by Benjamin Eberlei [ 14/Mar/13 ] |
|
Its indeed a good fix to disable the unique constraint if orphan removal isset. |
| Comment by Benjamin Eberlei [ 14/Mar/13 ] |
|
Fixed and merged into 2.3 With the change Doctrine will not create unique indexes anymore on Orphan Removal OneToOne associations. You need to change the database schema for this change to take effect essentially. |