Skip to content

Order of referenced child deletions not correct when referencing to itself #10348

@Toflar

Description

@Toflar

Bug Report

Q A
BC Break no
Version 2.14.0

Summary

The order in which the UoW wants to delete children is somehow missing a dependency when there is an association to the same class even if you specified cascade: ['remove'].
Given you have

  • Parent -> has an association to children with cascade: ['remove']
  • Child 1
  • Child 2 -> has an association to Child 1 with cascade: ['remove'].

Now you delete the parent.
Doctrine tries to remove Child 1 first instead of Child 2 causing a foreign constraint violation:

Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (test.child_entities, CONSTRAINT FK_DAEB194156A273CC FOREIGN KEY (origin_id) REFERENCES child_entities (id))

How to reproduce

I've created a test setup because I'm not familiar with the internals of ORM at all. So here we go:

  1. Clone https://github.com/Toflar/doctrine-orm-bug-reproducer
  2. Run composer install
  3. Adjust dummy mysql connection example in bootstrap.php.
  4. Run bin/doctrine orm:schema-tool:drop --force && bin/doctrine orm:schema-tool:create
  5. Run php test.php.

Expected behavior

It should not throw an exception. Doctrine should remove Child 2 first.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions