Closed
Description
Using Robots demo as the example;
If I have a Robots model, which is related to a Parts model via RobotsParts, and Robots has this relationship map;
$this->hasMany('id', 'RobotsParts', 'robots_id', ['alias' => 'RobotsParts']);
if I do
$robots = Robots::find();
$robots->delete();
I correctly get a PDOException
PDOException: SQLSTATE[23000]: Integrity constraint violation:
1451 Cannot delete or update a parent row: a foreign key constraint fails (`robots`.`robots_parts`, CONSTRAINT `robots_parts_ibfk_1` FOREIGN KEY (`robots_id`) REFERENCES `robots` (`id`))
however
if I alter the Robots relationship map to define a virtual foreign key with a friendlier message, eg
$this->hasMany('id', 'RobotsParts', 'robots_id', [
'alias' => 'RobotsParts',
'foreignKey' => ['message' => 'robot is associated to a part']
]);
then re-run
$robots = Robots::find();
$robots->delete();
$robots->delete() returns true, even though $robots->getMessages() has the FK message I defined. The actual database records are still correctly in place.
I would expect $robots->delete() to return false if any of the contained records fail to delete, in the same way as calling
$robot = Robots::findFirst();
$robot->delete();
will return false with the same FK violation.