DDC-1666: orphanRemoval does not work with oneToOne: Duplicate entry Error #2096

Closed
opened 2026-01-22 13:40:36 +01:00 by admin · 10 comments
Owner

Originally created by @doctrinebot on GitHub (Feb 23, 2012).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user gutzuwissen:

orphanRemoval does not work with oneToOne.
Im getting "duplicate entry" errors after replacing the oneToOne object.
Doctrine seems to insert the new data before removing the old one.
if i remove the unique constraint by hand in the database it works.

/*
Error:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1' for key 'UNIQ_9D8DDB05579B502F'
*/

/*
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;
    }
}
// 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();


Originally created by @doctrinebot on GitHub (Feb 23, 2012). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user gutzuwissen: orphanRemoval does not work with oneToOne. Im getting "duplicate entry" errors after replacing the oneToOne object. Doctrine seems to insert the new data before removing the old one. if i remove the unique constraint by hand in the database it works. ``` /* Error: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1' for key 'UNIQ_9D8DDB05579B502F' */ ``` ``` /* 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; } } ``` ``` // 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(); ```
admin added the Bug label 2026-01-22 13:40:36 +01:00
admin closed this issue 2026-01-22 13:40:42 +01:00
Author
Owner

@doctrinebot commented on GitHub (Feb 23, 2012):

@doctrinebot commented on GitHub (Feb 23, 2012): - is referenced by [DDC-3592: [GH-1318] Respect the "unique" property of the join column on the owning side of a...](http://www.doctrine-project.org/jira/browse/DDC-3592)
Author
Owner

@doctrinebot commented on GitHub (Mar 14, 2012):

Comment created by @beberlei:

This is a necessary restriction for the internals of Doctrine to always work correctly.

@doctrinebot commented on GitHub (Mar 14, 2012): Comment created by @beberlei: This is a necessary restriction for the internals of Doctrine to always work correctly.
Author
Owner

@doctrinebot commented on GitHub (Apr 12, 2012):

Comment created by acasademont:

+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.

@doctrinebot commented on GitHub (Apr 12, 2012): Comment created by acasademont: +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.
Author
Owner

@doctrinebot commented on GitHub (Apr 12, 2012):

Comment created by acasademont:

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.

@doctrinebot commented on GitHub (Apr 12, 2012): Comment created by acasademont: 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.
Author
Owner

@doctrinebot commented on GitHub (May 4, 2012):

Comment created by gutzuwissen:

+1
like Albert said, if its really a wont fix, the docs should state that.
the example in: http://readthedocs.org/docs/doctrine-orm/en/latest/reference/working-with-associations.html?highlight=orphan#orphan-removal with a (bidirectional) oneToOne orphanRemoval association isnt working with mysql and having doctrine created the tables.
maybe the bidirectional association is the problem?

@doctrinebot commented on GitHub (May 4, 2012): Comment created by gutzuwissen: +1 like Albert said, if its really a wont fix, the docs should state that. the example in: http://readthedocs.org/docs/doctrine-orm/en/latest/reference/working-with-associations.html?highlight=orphan#orphan-removal with a (bidirectional) oneToOne orphanRemoval association isnt working with mysql and having doctrine created the tables. maybe the bidirectional association is the problem?
Author
Owner

@doctrinebot commented on GitHub (Feb 11, 2013):

Comment created by mnapoli:

+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 [~beberlei].

@doctrinebot commented on GitHub (Feb 11, 2013): Comment created by mnapoli: +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 [~beberlei].
Author
Owner

@doctrinebot commented on GitHub (Mar 14, 2013):

Comment created by @beberlei:

Its indeed a good fix to disable the unique constraint if orphan removal isset.

@doctrinebot commented on GitHub (Mar 14, 2013): Comment created by @beberlei: Its indeed a good fix to disable the unique constraint if orphan removal isset.
Author
Owner

@doctrinebot commented on GitHub (Mar 14, 2013):

Comment created by @beberlei:

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.

@doctrinebot commented on GitHub (Mar 14, 2013): Comment created by @beberlei: 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.
Author
Owner

@doctrinebot commented on GitHub (Mar 14, 2013):

Issue was closed with resolution "Fixed"

@doctrinebot commented on GitHub (Mar 14, 2013): Issue was closed with resolution "Fixed"
Author
Owner

@zalesak commented on GitHub (Aug 1, 2023):

Am I doing something wrong or this is not resolved? I have doctrine/orm 2.16 and same error, same reason..

It looks like ClassMetadataInfo is setting 'unique' for join column therefore SchemaTool generates unique index on join column.

@zalesak commented on GitHub (Aug 1, 2023): Am I doing something wrong or this is not resolved? I have doctrine/orm 2.16 and same error, same reason.. It looks like ClassMetadataInfo is setting 'unique' for join column therefore SchemaTool generates unique index on join column.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#2096