Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

earmsby's avatar

Error when adding a new record via form

I'm getting an error and don't quite understand why or how to resolve. The error occurs when I try to create a new LibraryItem or edit an existing LibraryItem. Here is the error:

SQLSTATE[HY000]: General error: 20 datatype mismatch (Connection: sqlite, SQL: update "authors" set "id" = ?, "updated_at" = 2025-09-07 20:48:50 where "authors"."id" = 6 and "authors"."id" is not null)

The LibraryItem model has this relationship:

    public function composer(): HasOne
    {
        return $this->HasOne(Author::class,'id','composer_id');
    }

And my filament form has this select field:

Select::make('composer_id')
                        ->label('Composer')
                        ->searchable(['first_name', 'last_name'])
                        ->preload()
                        ->editOptionForm(Author::getForm())
                        ->createOptionForm(Author::getForm())
                        ->getOptionLabelFromRecordUsing(fn (Model $record) => "{$record->last_name}, {$record->first_name} ")
                        ->relationship('composer'),

I don't understand why submitting the LibraryItem create/edit form is attempting to update the authors table. I'm brand new to relationships in Filament so I'm guessing there's something I'm not quite understanding or doing correctly.

Thanks in advance for any advice!

0 likes
2 replies
LaryAI's avatar
Level 58

The error you’re seeing:

SQLSTATE[HY000]: General error: 20 datatype mismatch (Connection: sqlite, SQL: update "authors" set "id" = ?, "updated_at" = 2025-09-07 20:48:50 where "authors"."id" = 6 and "authors"."id" is not null)

suggests that Filament is trying to update the id column of the authors table, which is not what you want. This usually happens due to a misconfiguration in the relationship definition and how Filament is handling the select field.

What’s Happening?

  • Your composer relationship is defined as:

    public function composer(): HasOne
    {
        return $this->HasOne(Author::class,'id','composer_id');
    }
    

    This means: "This LibraryItem has one Author, where the Author's id matches LibraryItem's composer_id."

  • In your Filament form, you use:

    ->relationship('composer')
    

    This tells Filament to use the composer relationship for this select field. However, Filament expects a BelongsTo relationship for this kind of select, not a HasOne.

Why is it trying to update the authors table?

Because with a HasOne relationship, Filament thinks the current model (LibraryItem) is the parent, and it tries to update the related model (Author) to point back to the parent. That’s why it tries to update the id of the author, which is not what you want.


Solution

You should define the relationship as a BelongsTo in your LibraryItem model:

public function composer(): BelongsTo
{
    return $this->belongsTo(Author::class, 'composer_id');
}
  • This means: "This LibraryItem belongs to an Author, referenced by composer_id."

Now, your Filament select field will work as expected, and it will set the composer_id on the library_items table, not try to update the authors table.


Summary of Steps

  1. Update your model relationship:

    use Illuminate\Database\Eloquent\Relations\BelongsTo;
    
    public function composer(): BelongsTo
    {
        return $this->belongsTo(Author::class, 'composer_id');
    }
    
  2. Keep your Filament select field as is:

    Select::make('composer_id')
        ->label('Composer')
        ->searchable(['first_name', 'last_name'])
        ->preload()
        ->editOptionForm(Author::getForm())
        ->createOptionForm(Author::getForm())
        ->getOptionLabelFromRecordUsing(fn (Model $record) => "{$record->last_name}, {$record->first_name} ")
        ->relationship('composer')
    

After this change, Filament will correctly handle the relationship and update the composer_id on your LibraryItem record, not the authors table.

Let me know if you have any further questions!

1 like
earmsby's avatar

That did the trick (in case anyone else runs into this)!

Please or to participate in this conversation.