7 months ago

UUID and Pivot Tables

Posted 7 months ago by kvnkrft

I found the article about UUID from @wilburpowery (https://wilburpowery.co/posts/easy-uuids-in-laravel/) and everything is working great, other than attaching. The error message is...

SQLSTATE[HY000]: General error: 1364 Field 'id' doesn't have a default value (SQL: insert into `deal_category_pivot` (`category_id`, `deal_id`) values (b4fca70c-ba7b-4ad8-99d9-0908aa6837df, 7439b146-cd29-4aa3-9718-bfc93707724d)) 

which makes perfect sense. I just don't know how to modify the trait to take care of this. Any thoughts?

Here is the trait, as per the article...


namespace App\Traits;

use Illuminate\Support\Str;

trait UsesUuid
    protected static function bootUsesUuid()
        static::creating(function ($model) {
            if (! $model->getKey()) {
                $model->{$model->getKeyName()} = (string) Str::uuid();

    public function getIncrementing()
        return false;

    public function getKeyType()
        return 'string';

Please sign in or create an account to participate in this conversation.