Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto add better constraint actions #385

Open
1 task done
dereuromark opened this issue Feb 15, 2019 · 4 comments
Open
1 task done

Auto add better constraint actions #385

dereuromark opened this issue Feb 15, 2019 · 4 comments

Comments

@dereuromark
Copy link
Member

dereuromark commented Feb 15, 2019

This is a (multiple allowed):

  • 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.

@karmicdice
Copy link
Contributor

karmicdice commented Jul 23, 2019

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.

@markstory markstory added this to the 2.x milestone Jul 24, 2019
@dereuromark
Copy link
Member Author

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
Copy link
Member Author

@markstory Do you think this could be done in a reasonable enough way?
Or is this too complicated?

@markstory
Copy link
Member

I'm not sure I understand the scenario well. Is the situation that you have model relations defined, but no migration. Then when a migration is generated it should use model relations to infer the schema?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants