user11056335
user11056335

Reputation:

Foreign key and cascade problems on delete Symfony4/Doctrine

I want to delete a property with my user but I have the following error :

An exception occurred while executing 'DELETE FROM property WHERE id = ?' with params [1]:

SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (gestImmo.equipment, CONSTRAINT FK_D338D583517FE9FE FOREIGN KEY (equipment_id) REFERENCES property (id))

I googled it and (I think) it's a cascade problem. So I searched on forums but I didn't solved the problem. I asked for help to an experimented coworker but we didn't fix the mistake ... Hope you could help me.

In my User entity there is :

     /**
     * @ORM\OneToMany(targetEntity="App\Entity\Property", mappedBy="userProperty")
     */
    private $properties;

In my property Entity there is :

     /**
     * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="properties")
     * @JoinColumn(name="id", referencedColumnName="id", onDelete="CASCADE")
     */
    private $userProperty;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Equipment", mappedBy="equipment")
     * @JoinColumn(name="id", referencedColumnName="equipement_id", onDelete="CASCADE")
     */
    private $equipments;

and in my equipments entity there is :

     /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Property", inversedBy="equipments")
     * @Assert\Type("string")
     * @var string
     */
    private $equipment;

Thanks for your help !

Upvotes: 1

Views: 6130

Answers (2)

SpicyTacos23
SpicyTacos23

Reputation: 550

Setting the onDelete="CASCADE" in your manyToOne annotation should be enough.

Don't forget to bin/console make:migration after that. Everything you change in your ORM must be migrated in order to apply the changes into the database.

Upvotes: 3

Arthur Shveida
Arthur Shveida

Reputation: 457

You cannot delete property because it is referenced in column equipment_id in table equipment. This foreign key constraint means that equipment.equipment_id must point to a valid(existing) id of property - property.id. If you want to delete property, then before that you either:

  • Delete equipment records where equipment_id = {id of property you want to delete}
  • Or change those equipment_id to null

EDIT

Looks like your annotations are incorrect. If I understood your relations correctly, then it should be something like this.

Property entity:

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="properties")
     * @JoinColumn(name="user_property_id", referencedColumnName="id", onDelete="CASCADE")
     */
    private $userProperty;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Equipment", mappedBy="equipment")
     */
    private $equipments;

Equipment entity:

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Property", inversedBy="equipments")
     * @ORM\JoinColumn(name="equipment_id", referencedColumnName="id", onDelete="CASCADE")
     */
    private $equipment;

Upvotes: 1

Related Questions