DDC-1974: @OneToOne and @InheritanceType cause Duplicate Key Errors when changing the entity #2493

Closed
opened 2026-01-22 13:55:07 +01:00 by admin · 3 comments
Owner

Originally created by @doctrinebot on GitHub (Aug 9, 2012).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user pdobrigkeit:

<?php

/*** @Entity **/
class A
{
    /****
     * @Id @Column(name="id", type="integer")
     */
    private $id;

    /****
     * @OneToOne(targetEntity="B", mappedBy="a")
     */
    private $b;
}

/**** 
 * @Entity 
 * @InheritanceType("SINGLE_TABLE")
 */
class B
{
    /****
     * @Id @Column(name="id", type="integer", nullable=false)
     */
    private $id;

    /****
     * @OneToOne(targetEntity="A", inversedBy="b")
     */
    private $a;
}

/**** 
 * @Entity 
 */
class B_sub extends B
{
    /*** @Column(type="string") **/
    private $sub;
}

/**** 
 * @Entity 
 */
class B_sub2 extends B
{
    /*** @Column(type="string") **/
    private $sub2;
}

The code creates several tables and a UNIQUE KEY constraint on B (a_id)

Now trying to accomplish the following sequence of action:

$B = new B_sub();
$A = new A();

$A->set('b', $B);
$B->set('a', $A);

$em->persist($A);
$em->persist($B);
//works fine and creates the correct DB entries
$em->flush();

$B*new = new B*sub2();
$B_old = $A->get('b');
$A->set('b', $B_new);
$B_new->set('a', $A);

$em->remove($B_old);
$em->persist($A);
$em->persist($B_new);
//does not work, because B*new is inserted first and fails the UNIQUE KEY constraint (both B_new and B*old refer to A)
$em->flush();

The result is a "Duplicate entry '1' for key 'UNIQ_7F6BFCEBE26CCE03'"

Originally created by @doctrinebot on GitHub (Aug 9, 2012). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user pdobrigkeit: ``` xml <?php /*** @Entity **/ class A { /**** * @Id @Column(name="id", type="integer") */ private $id; /**** * @OneToOne(targetEntity="B", mappedBy="a") */ private $b; } /**** * @Entity * @InheritanceType("SINGLE_TABLE") */ class B { /**** * @Id @Column(name="id", type="integer", nullable=false) */ private $id; /**** * @OneToOne(targetEntity="A", inversedBy="b") */ private $a; } /**** * @Entity */ class B_sub extends B { /*** @Column(type="string") **/ private $sub; } /**** * @Entity */ class B_sub2 extends B { /*** @Column(type="string") **/ private $sub2; } ``` The code creates several tables and a UNIQUE KEY constraint on B (a_id) Now trying to accomplish the following sequence of action: ``` xml $B = new B_sub(); $A = new A(); $A->set('b', $B); $B->set('a', $A); $em->persist($A); $em->persist($B); //works fine and creates the correct DB entries $em->flush(); $B*new = new B*sub2(); $B_old = $A->get('b'); $A->set('b', $B_new); $B_new->set('a', $A); $em->remove($B_old); $em->persist($A); $em->persist($B_new); //does not work, because B*new is inserted first and fails the UNIQUE KEY constraint (both B_new and B*old refer to A) $em->flush(); ``` The result is a "Duplicate entry '1' for key 'UNIQ_7F6BFCEBE26CCE03'"
admin added the Bug label 2026-01-22 13:55:07 +01:00
admin closed this issue 2026-01-22 13:55:07 +01:00
Author
Owner

@doctrinebot commented on GitHub (Aug 10, 2012):

Comment created by pdobrigkeit:

Current workaround is to change the ownership of the relationship

<?php

/*** @Entity **/
class A
{
    /****
     * @Id @Column(name="id", type="integer")
     */
    private $id;

    /****
     * @OneToOne(targetEntity="B", inversedBy="a")
     */
    private $b;
}

/**** 
 * @Entity 
 * @InheritanceType("SINGLE_TABLE")
 */
class B
{
    /****
     * @Id @Column(name="id", type="integer", nullable=false)
     */
    private $id;

    /****
     * @OneToOne(targetEntity="A", mappedBy="b")
     */
    private $a;
}
@doctrinebot commented on GitHub (Aug 10, 2012): Comment created by pdobrigkeit: Current workaround is to change the ownership of the relationship ``` xml <?php /*** @Entity **/ class A { /**** * @Id @Column(name="id", type="integer") */ private $id; /**** * @OneToOne(targetEntity="B", inversedBy="a") */ private $b; } /**** * @Entity * @InheritanceType("SINGLE_TABLE") */ class B { /**** * @Id @Column(name="id", type="integer", nullable=false) */ private $id; /**** * @OneToOne(targetEntity="A", mappedBy="b") */ private $a; } ```
Author
Owner

@doctrinebot commented on GitHub (Aug 29, 2012):

Comment created by @beberlei:

Insert before Removal is by design, see discussion here http://www.doctrine-project.org/jira/browse/DDC-601

@doctrinebot commented on GitHub (Aug 29, 2012): Comment created by @beberlei: Insert before Removal is by design, see discussion here http://www.doctrine-project.org/jira/browse/[DDC-601](http://www.doctrine-project.org/jira/browse/DDC-601)
Author
Owner

@doctrinebot commented on GitHub (Aug 29, 2012):

Issue was closed with resolution "Won't Fix"

@doctrinebot commented on GitHub (Aug 29, 2012): Issue was closed with resolution "Won't Fix"
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#2493