migrations icon indicating copy to clipboard operation
migrations copied to clipboard

Auto add better constraint actions

Open dereuromark opened this issue 6 years ago • 4 comments

This is a (multiple allowed):

  • [x] enhancement

  • CakePHP Version: latest

What you did

Deleting a parent of child, that do not have "depending=>true" relationship. As such, the foreign_key was still the old id, we just removed. Now there is an invalid foreign key set that doesnt exist as row anymore.

Expected Behavior

When generating migrations (bake task and otherwise):

'delete' => 'SET_NULL' for addForeignKey() should be auto-set for such "NULLable" fields that have no such depending constraint. This would make sure the foreign_key here is set to NULL again and when we query for all that have no relation to that parent we would also find the right entities.

Example code

    $this->table('releases')
        ->addForeignKey('release_element_id', 'release_elements', ['id'], ['delete' => 'SET_NULL'])
        ->update();

For a DEFAULT NULL relation here.

dereuromark avatar Feb 15 '19 14:02 dereuromark

This would make sure the foreign_key here is set to NULL again

Tried replicating this behaviour. What I noticed is. If the column is not nullable, it throws an error.

Please elucidate.

karmicdice avatar Jul 23 '19 20:07 karmicdice

Yes, this is only meant for nullable colums to keep data integrity. If not nullable, the parent delete must delete also the children (via constraint or domain layer logic / relation setup).

dereuromark avatar Feb 21 '24 06:02 dereuromark