filament: Unable to Create Entity When Using HasOne Relationship

Package

filament/filament

Package Version

v2.13.24

Laravel Version

v9.19.0

Livewire Version

v2.10.6

PHP Version

PHP 8.0.19

Bug description

I followed the instructions at https://filamentphp.com/docs/2.x/forms/layout#saving-data-to-relationships to create the following form schema which should save user data while creating the employee and creating the relationship:

class EmployeeResource extends Resource
{
    protected static ?string $model = Employee::class;

    protected static ?string $navigationIcon = 'heroicon-o-collection';

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Fieldset::make('Employee Data')
                    ->schema([
                        TextInput::make('employee_code')->required(),
                    ]),
                Fieldset::make('User')
                    ->relationship('user')
                    ->schema([
                        TextInput::make('name')->required(),
                        TextInput::make('email')->email()->required(),
                        TextInput::make('password')->password()->required(),
                    ])
            ]);
    }

But I keep getting this error:

SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value
insert into `employees` (`employee_code`, `updated_at`, `created_at`) values (TEST, 2022-07-08 20:49:42, 2022-07-08 20:49:42)
image

Any ideas on what I am doing wrong here?

Steps to reproduce

  1. Create a model named Employee with hasOne relationship with User model
  2. Create EmployeeResource using make:filament-resource
  3. Add the following form schema to EmployeeResource as per https://filamentphp.com/docs/2.x/forms/layout#saving-data-to-relationships
    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Fieldset::make('Employee Data')
                    ->schema([
                        TextInput::make('employee_code')->required(),
                    ]),
                Fieldset::make('User')
                    ->relationship('user')
                    ->schema([
                        TextInput::make('name')->required(),
                        TextInput::make('email')->email()->required(),
                        TextInput::make('password')->password()->required(),
                    ])
            ]);
    }
  1. Run php artisan serve
  2. Go to http://127.0.0.1:8000/admin/employees/create and submit
  3. Error showing on screen
SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value
insert into `employees` (`employee_code`, `updated_at`, `created_at`) values (TEST, 2022-07-08 20:49:42, 2022-07-08 20:49:42)
image

Reproduction repository

https://github.com/Uolsen/filament-hasone-bug

Relevant log output

SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value
insert into `employees` (`employee_code`, `updated_at`, `created_at`) values (TEST, 2022-07-08 20:49:42, 2022-07-08 20:49:42)

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 16 (9 by maintainers)

Most upvoted comments

Fixed by #3524.

I will pin link also there:

https://github.com/Uolsen/filament-hasone-bug

In my spare time i will try to find bug. But it will be great if someone more acknowledged will look at it.

@zepfietje It has been quite some time and I moved on with another work around. I will try to setup a demo of the exact issue sometime early next week for you to review and replicate.

It would be great if anyone else facing this issue freshly can help with this aswell.

Has anyone actually tried to debug it? It would really help us out, as there are other issues that need our attention too.