(doctrine2 + symfony2) cascading remove : integrity constraint violation 1451
So, thanks to this French forum, I fixed the problem.
I needed to add nullable=true & onDelete="SET NULL" in @ORM\JoinColumn
Here is the workable configuration, maybe it will help someone :
#User. /** * @ORM\OneToMany(targetEntity="\sfCommands\ContentBundle\Entity\Application", mappedBy="user", cascade={"remove"}, orphanRemoval=true) * @ORM\OrderBy({"name" = "ASC"}) */ protected $applications; /** * @ORM\OneToOne(targetEntity="\sfCommands\ContentBundle\Entity\Entity") * @ORM\JoinColumn(name="entity1_id", referencedColumnName="id", nullable=true, onDelete="SET NULL") */ private $entity1; /** * @ORM\OneToOne(targetEntity="\sfCommands\ContentBundle\Entity\Entity") * @ORM\JoinColumn(name="entity2_id", referencedColumnName="id", nullable=true, onDelete="SET NULL") */ private $entity2;#Application. /** * @ORM\OneToMany(targetEntity="\sfCommands\ContentBundle\Entity\Bundle", mappedBy="application", cascade={"remove"}, orphanRemoval=true) * @ORM\OrderBy({"name" = "ASC"}) */ protected $bundles; /** * @ORM\ManyToOne(targetEntity="\sfCommands\UserBundle\Entity\User", inversedBy="applications", cascade={"persist"}) * @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=true, onDelete="SET NULL") */ protected $user;#Bundle. /** * @ORM\ManyToOne(targetEntity="\sfCommands\ContentBundle\Entity\Application", inversedBy="bundles", cascade={"persist"}) * @ORM\JoinColumn(name="application_id", referencedColumnName="id", nullable=true, onDelete="SET NULL") */ protected $application; /** * @ORM\OneToMany(targetEntity="\sfCommands\ContentBundle\Entity\Entity", mappedBy="bundle", cascade={"remove"}, orphanRemoval=true) * @ORM\OrderBy({"name" = "ASC"}) */ protected $entitys;#Entity. /** * @ORM\ManyToOne(targetEntity="\sfCommands\ContentBundle\Entity\Bundle", inversedBy="entitys", cascade={"persist"}) * @ORM\JoinColumn(name="bundle_id", referencedColumnName="id", nullable=true, onDelete="SET NULL") */ protected $bundle;
Use onDelete="CASCADE" if you are using annotation
/** * @ORM\ManyToOne(targetEntity="Report", inversedBy="responses") * @ORM\JoinColumn(name="reportId", referencedColumnName="id",onDelete="CASCADE") */
Use onDelete: CASCADE if you are using yml
joinColumn: name: pid referencedColumnName: id onDelete: CASCADE
onDelete="CASCADE" also works fine. But don't forget to run app/console doctrine:schema:update --force before DB level changes will take an effect.