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

Closed
doctrinebot opened this Issue Aug 9, 2012 · 3 comments

2 participants

@doctrinebot

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'"

@doctrinebot

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

Issue was closed with resolution "Won't Fix"

@beberlei beberlei was assigned by doctrinebot Dec 6, 2015
@doctrinebot doctrinebot closed this Dec 6, 2015
@doctrinebot doctrinebot added the Bug label Dec 7, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment