UniqueConstraintViolationException throwed when calling clear() on a collection owned by an entity with DEFERRED_EXPLICIT change tracking policy #6441

Open
opened 2026-01-22 15:33:19 +01:00 by admin · 0 comments
Owner

Originally created by @mmonsang on GitHub (Apr 2, 2020).

BUG report

Q A
BC Break yes
Version 2.6.4

Summary

Since version 2.6.4, and the resolution of #7758 :
Calling clear() on a Many-to-many association collection owned by an entity with DEFERRED_EXPLICIT change tracking policy could provoke UniqueConstraintViolationException.

How to reproduce

Mapping :

<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping">
    <entity name="Foo"
            change-tracking-policy="DEFERRED_EXPLICIT">
        <id name="id" type="integer" column="id">
            <generator strategy="IDENTITY"/>
        </id>
        <many-to-many field="bars" target-entity="Bar">
            <cascade>
                <cascade-all/>
            </cascade>
            <join-table name="foo_bar">
                <join-columns>
                    <join-column name="foo_id" nullable="false"/>
                </join-columns>
                <inverse-join-columns>
                    <join-column name="bar_id" nullable="false"/>
                </inverse-join-columns>
            </join-table>
        </many-to-many>
    </entity>
    <entity name="Bar"
            change-tracking-policy="DEFERRED_EXPLICIT">
        <id name="id" type="integer" column="id">
            <generator strategy="IDENTITY"/>
        </id>
    </entity>
</doctrine-mapping>

with PHP code :

class Bar { public $id; }
class Foo {
    public $id;
    public $bars;
    public function __construct()
    {
        $this->bars = new ArrayCollection();
    }
}

// Create new entity
$bar = new Bar();
$foo = new Foo();
// Attach Bar
$foo->bars->add($bar);
// Persist the whole thing
$em->persist($foo);
$em->flush();

// Retrieve assigned id
$fooId = $foo->id;

// Fetch from DB
$foo = $em->find(Foo::class, $fooId);
$foo->bars->clear();
$foo->bars->add($bar);
$em->persist($foo);
$em->flush();

Result

throw a PDOException\PDOException\UniqueConstraintViolationException

Expected behavior

No exception should be throwed.

Originally created by @mmonsang on GitHub (Apr 2, 2020). ## BUG report Q | A -- | -- BC Break | yes Version | 2.6.4 **Summary** Since version 2.6.4, and the resolution of #7758 : Calling clear() on a Many-to-many association collection owned by an entity with DEFERRED_EXPLICIT change tracking policy could provoke UniqueConstraintViolationException. **How to reproduce** Mapping : ```xml <doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"> <entity name="Foo" change-tracking-policy="DEFERRED_EXPLICIT"> <id name="id" type="integer" column="id"> <generator strategy="IDENTITY"/> </id> <many-to-many field="bars" target-entity="Bar"> <cascade> <cascade-all/> </cascade> <join-table name="foo_bar"> <join-columns> <join-column name="foo_id" nullable="false"/> </join-columns> <inverse-join-columns> <join-column name="bar_id" nullable="false"/> </inverse-join-columns> </join-table> </many-to-many> </entity> <entity name="Bar" change-tracking-policy="DEFERRED_EXPLICIT"> <id name="id" type="integer" column="id"> <generator strategy="IDENTITY"/> </id> </entity> </doctrine-mapping> ``` with PHP code : ```php class Bar { public $id; } class Foo { public $id; public $bars; public function __construct() { $this->bars = new ArrayCollection(); } } // Create new entity $bar = new Bar(); $foo = new Foo(); // Attach Bar $foo->bars->add($bar); // Persist the whole thing $em->persist($foo); $em->flush(); // Retrieve assigned id $fooId = $foo->id; // Fetch from DB $foo = $em->find(Foo::class, $fooId); $foo->bars->clear(); $foo->bars->add($bar); $em->persist($foo); $em->flush(); ``` **Result** throw a `PDOException\PDOException\UniqueConstraintViolationException` **Expected behavior** No exception should be throwed.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6441