I get a violation of external constraints when I try to delete an object containing one-to-many unidirectional associations. I have the following simple class:
class Dealer{ /** * @ManyToMany(targetEntity="Car", cascade={"persist", "remove"}) * @JoinTable(name="dealer_cars", * joinColumns={@JoinColumn(name="dealer_id", referencedColumnName="id")}, * inverseJoinColumns={@JoinColumn(name="car_id", referencedColumnName="id", unique=true)} * ) **/ protected cars; }
The Car object should not contain a relationship to its owner in this case (hence the unidirectional relationship). If I try to delete a Dealer object containing associations with machines, I get the following restriction violation:
Cannot delete or update a parent row: a foreign key constraint fails (`application`.`dealer_cars`, CONSTRAINT `FK_E1BCEEEBC3C6F69F` FOREIGN KEY (`car_id`) REFERENCES `car` (`id`))'
I would get the same message if I tried to delete the dealer row manually from the database table, but I thought that Doctrine, using cascade = "remove", would take care of this for me.
If I change the association to bidirectional communication, it works. Why does this not work with unidirectional associations?
php orm doctrine2
Frederik wordenskjold
source share