Skip to content

Loading…

DDC-959: Problem deleting when the foreign key is the primary key of the table #5493

Closed
doctrinebot opened this Issue · 3 comments

2 participants

@doctrinebot

Jira issue originally created by user ngoc:

when i wan to delete Element

$element =  $this->entityManager->find('Element', $id);
entityManager->remove($entity);
entityManager->flush();

I have this error "Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails .. CONSTRAINT Element*ibfk_2 FOREIGN KEY (id*parent) REFERENCES Element (id)."

Do you have any explanation on this problem please?

There is my classe :

<?php

namespace Entities;

/****
 * @Entity
 */
class Element
{
    /*** @Id @Column(type="integer") @GeneratedValue **/
    protected $id;

    /****
    * @Column(type="integer", nullable=false)
    */
    protected $num_order;


    /****
    * @Column(type="string", length=50, nullable=false)
    */
    protected $name;

    /****
     * @Column(type="boolean")
     */
    protected $active;

    /****
     * @OneToMany(targetEntity="Entities\Element", mappedBy="parent", cascade={"remove"}, orphanRemoval=true)
     */
    protected $children;


     /****
     * @ManyToOne(targetEntity="Entities\Element", inversedBy="children")
     * @JoinColumn(name="id_parent", referencedColumnName="id")
     */
    protected $parent;

    /****
     * Get id
     *
     * @return integer $id
     */
    public function getId()
    {
        return $this->id;
    }

    /****
     * Set num_order
     *
     * @param integer $numOrder
     */
    public function setNumOrder($numOrder)
    {
        $this->num_order = $numOrder;
    }

    /****
     * Get num_order
     *
     * @return integer $numOrder
     */
    public function getNumOrder()
    {
        return $this->num_order;
    }

    /****
     * Set name
     *
     * @param string $name
     */
    public function setName($name)
    {
        $this->name = $name;
    }

    /****
     * Get name
     *
     * @return string $name
     */
    public function getName()
    {
        return $this->name;
    }

    /****
     * Set active
     *
     * @param boolean $active
     */
    public function setActive($active)
    {
        $this->active = $active;
    }

    /****
     * Get active
     *
     * @return boolean $active
     */
    public function getActive()
    {
        return $this->active;
    }

    /****
     * Add children
     *
     * @param Entities\Element $children
     */
    public function addChildren(\Entities\Element $children)
    {
        $this->children[] = $children;
    }

    /****
     * Get children
     *
     * @return Doctrine\Common\Collections\Collection $children
     */
    public function getChildren()
    {
        return $this->children;
    }

    /****
     * Set parent
     *
     * @param Entities\Element $parent
     */
    public function setParent(\Entities\Element $parent)
    {
        $this->parent = $parent;
    }

    /****
     * Get parent
     *
     * @return Entities\Element $parent
     */
    public function getParent()
    {
        return $this->parent;
    }
}
@doctrinebot

Comment created by @beberlei:

Set a join column and have it set onDelete = "SET NULL"

@doctrinebot

Issue was closed with resolution "Invalid"

@beberlei beberlei was assigned by doctrinebot
@doctrinebot doctrinebot closed this
@doctrinebot doctrinebot added the Bug label
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.