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:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`db`.`pages`, CONSTRAINT `pages_parent_id_foreign` FOREIGN KEY (`parent_id`) REFERENCES `pages` (`id`))
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`db`.`pages`, CONSTRAINT `pages_parent_id_foreign` FOREIGN KEY (`parent_id`) REFERENCES `pages` (`id`))
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.
protected function mutateFormDataBeforeSave(array $data): array
{
$data['parent_id'] = $data['parent_id'] === '' ? null : $data['parent_id'];

return $data;
}
protected function mutateFormDataBeforeSave(array $data): array
{
$data['parent_id'] = $data['parent_id'] === '' ? null : $data['parent_id'];

return $data;
}
Or is there an error in my relationships? Migration:
$table->foreignUlid('parent_id')
->nullable()
->constrained('pages')
->cascadeOnUpdate()
->cascadeOnDelete();
$table->foreignUlid('parent_id')
->nullable()
->constrained('pages')
->cascadeOnUpdate()
->cascadeOnDelete();
Page model:
public function parent(): BelongsTo
{
return $this->BelongsTo(Page::class, 'parent_id');
}
public function parent(): BelongsTo
{
return $this->BelongsTo(Page::class, 'parent_id');
}
Thank you for any tips or hints.
Solution:
Don’t you want to nullOnDelete() instead of cascadeOnDelete() here? See https://laravel.com/docs/12.x/migrations#foreign-key-constraints
Database: 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.
Jump to solution
4 Replies
Solution
Tim van Heugten
Tim van Heugten4mo ago
Don’t you want to nullOnDelete() instead of cascadeOnDelete() here? See https://laravel.com/docs/12.x/migrations#foreign-key-constraints
Database: 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.
Mike Wink
Mike WinkOP4mo ago
Getting the same error for SQLite. :/
Tim van Heugten
Tim van Heugten4mo ago
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?
Mike Wink
Mike WinkOP4mo ago
@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. 👍🏼👍🏼

Did you find this page helpful?