Setting a relationship back to null
I have a v4 beta10 installation, of Filament, where I use a simple page model/resource. This model has a self referenced parent_id field. In my edit form, I use a select field with a relationship. There I show all pages (except of the currently edited one). I can successfully add and change parent pages. What is not working is removing a parent page. Then I got this error:
Setting the field back to
null
works on a DB level. So I guess it is correctly set up.
The question is, how can I make Filament set null
? Currently, I am resolving the problem by using this code in the EditPage class.
Or is there an error in my relationships?
Migration:
Page model:
Thank you for any tips or hints.Solution:Jump to solution
Don’t you want to
nullOnDelete()
instead of cascadeOnDelete()
here? See https://laravel.com/docs/12.x/migrations#foreign-key-constraintsDatabase: Migrations - Laravel 12.x - The PHP Framework For Web Art...
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
4 Replies
Solution
Don’t you want to
nullOnDelete()
instead of cascadeOnDelete()
here? See https://laravel.com/docs/12.x/migrations#foreign-key-constraintsDatabase: Migrations - Laravel 12.x - The PHP Framework For Web Art...
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
Getting the same error for SQLite. :/
Not sure what you mean. You tell it to cascade on delete, meaning its trying to delete the row when the parent is deleted. You want the field to become null when the parent is deleted, so I suggested to use ‘nullOnDelete’. I never touched SQLite but I can only assume it behaves the same?
@Tim van Heugten I changed the code as you suggested, and it did throw the same error after re-migrating the DB tables. I will check if there is different behavior in MySQL and SQLite.
Definitely a big thank you for the suggestion and clarification. 👍🏼👍🏼