doctrine / data-fixtures

Doctrine2 ORM Data Fixtures Extensions
http://www.doctrine-project.org
MIT License
2.78k stars 224 forks source link

Cannot delete or update a parent row: a foreign key constraint fails #261

Closed fezfez closed 7 years ago

fezfez commented 7 years ago

composer info

doctrine/annotations                         v1.5.0  Docblock Annotations Parser
doctrine/cache                               v1.7.0  Caching library offering an object-oriented API for many cache backends
doctrine/collections                         v1.5.0  Collections Abstraction library
doctrine/common                              v2.7.3  Common Library for Doctrine projects
doctrine/data-fixtures                       v1.2.2  Data Fixtures for all Doctrine Object Managers
doctrine/dbal                                v2.5.13 Database Abstraction Layer
doctrine/doctrine-module                     1.2.0   Zend Framework Module that provides Doctrine basic functionality required for ORM and ODM modules
doctrine/doctrine-orm-module                 1.1.3   Zend Framework Module that provides Doctrine ORM functionality
doctrine/inflector                           v1.2.0  Common String Manipulations with regard to casing and singular/plural rules.
doctrine/instantiator                        1.0.5   A small, lightweight utility to instantiate objects in PHP without invoking their constructors
doctrine/lexer                               v1.0.1  Base library for a lexer that can be used in Top-Down, Recursive Descent Parsers.
doctrine/orm                                 v2.5.6  Object-Relational-Mapper for PHP

Entity


/**
 * Tiers
 * @ORM\Entity
 * @ORM\Table(name="users_tiers")
 */
class UsersTiers
{
...
    /**
     * @var \Tiers\Entity\Tiers
     *
     * @ORM\ManyToOne(targetEntity="Tiers\Entity\Tiers", inversedBy="users")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="tiers", referencedColumnName="id")
     * })
     */
    private $tiers;
....
}
/**
 * Tiers
 *
 * @ORM\Table(name="tiers")
 * @ORM\Entity()
 */
class Tiers
{
...

    /**
     * @var Users
     *
     * @ORM\ManyToOne(targetEntity="User\User\UsersTiers")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="creator", referencedColumnName="id", nullable=true)
     * })
     */
    private $creator;

    /**
     * @var Doctrine\Common\Collections\Collection
     *
     * @ORM\OneToMany(targetEntity="User\User\UsersTiers",  mappedBy="tiers" )
     **/
    private $users;

...

After calling

        $purger = new ORMPurger();
        $executor = new ORMExecutor(ServiceManagerFactory::getEm(), $purger);

        $executor->purge();

I got this error

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (cube43.users_tiers, CONSTRAINT FK_F1309FEB16473BA2 FOREIGN KEY (tiers) REFERENCES tiers (id))

Ocramius commented 7 years ago

This cannot be fixed by the purger, as there is a dependency loop in your entity relations, if I read this correctly.

On 29 Jul 2017 7:03 PM, "Stéphane" notifications@github.com wrote:

composer info

doctrine/annotations v1.5.0 Docblock Annotations Parser doctrine/cache v1.7.0 Caching library offering an object-oriented API for many cache backends doctrine/collections v1.5.0 Collections Abstraction library doctrine/common v2.7.3 Common Library for Doctrine projects doctrine/data-fixtures v1.2.2 Data Fixtures for all Doctrine Object Managers doctrine/dbal v2.5.13 Database Abstraction Layer doctrine/doctrine-module 1.2.0 Zend Framework Module that provides Doctrine basic functionality required for ORM and ODM modules doctrine/doctrine-orm-module 1.1.3 Zend Framework Module that provides Doctrine ORM functionality doctrine/inflector v1.2.0 Common String Manipulations with regard to casing and singular/plural rules. doctrine/instantiator 1.0.5 A small, lightweight utility to instantiate objects in PHP without invoking their constructors doctrine/lexer v1.0.1 Base library for a lexer that can be used in Top-Down, Recursive Descent Parsers. doctrine/orm v2.5.6 Object-Relational-Mapper for PHP

Entity

/ Tiers @ORM\Entity @ORM\Table(name="users_tiers") /class UsersTiers{... / @var \Tiers\Entity\Tiers @ORM\ManyToOne(targetEntity="Tiers\Entity\Tiers", inversedBy="users") @ORM\JoinColumns({ @ORM\JoinColumn(name="tiers", referencedColumnName="id") }) */ private $tiers;....}/ Tiers @ORM\Table(name="tiers") @ORM\Entity() */class Tiers{... /* @var Users @ORM\ManyToOne(targetEntity="User\User\UsersTiers") @ORM\JoinColumns({ @ORM\JoinColumn(name="creator", referencedColumnName="id", nullable=true) }) / private $creator; / @var Doctrine\Common\Collections\Collection * @ORM\OneToMany(targetEntity="User\User\UsersTiers", mappedBy="tiers" ) **/ private $users;...

After calling

    $purger = new ORMPurger();        $executor = new ORMExecutor(ServiceManagerFactory::getEm(), $purger);        $executor->purge();

I got this error

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (cube43. users_tiers, CONSTRAINT FK_F1309FEB16473BA2 FOREIGN KEY (tiers) REFERENCES tiers (id))

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/doctrine/data-fixtures/issues/261, or mute the thread https://github.com/notifications/unsubscribe-auth/AAJakKG7RlJkKEMA8PsKOLkJLZqefeqkks5sS2XBgaJpZM4OnaDE .

fezfez commented 7 years ago

So I have to delete them manually ? In your opinion, this is solvable automatically?

Ocramius commented 7 years ago

This can probably be solved automatically only by defining a DB-side cascade persist operation.

fezfez commented 7 years ago

Thanks for the explaination, it's fixed :)

pascalwacker commented 6 years ago

@fezfez if you still remember, could you please elaborate on how you fixed this? thanks.