-
Notifications
You must be signed in to change notification settings - Fork 11.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Missing the primary key (id) on the model when using create #26673
Comments
What type of column is |
What's in the database table? Where are the uuids generated? |
The default is to use the postgresql generated uuids. The migration looks as follows: Schema::create('roles', function (Blueprint $table) {
$table->uuid('id')
->default(DB::raw('gen_random_uuid()'));
$table->primary('id');
$table->string('name');
$table->string('display_name')->nullable();
$table->string('description')->nullable();
// Unique key is set using an DB::statement
//$table->unique(['name', 'deleted_at']);
$table->timestamps();
$table->softDeletes();
}); |
You must define value of the public static function boot()
{
parent::boot();
static::creating(function ($model) {
$model->setAttribute('id', Str::uuid());
});
} |
I agree with @dinhquochan 's solution. However, I would put the logic in a trait, so it can be reused for other models easily. |
@matthijs @CrixuAMG something like that spatie/laravel-binary-uuid |
@matthijs did it work with @dinhquochan's solution? |
@driesvints I'll test it when back at home. |
@dinhquochan exactly, but for such a little thing creating your own trait would be better imho. |
Until 5.7.15 (just opened an issue about it) you could just leave |
@zecipriano ah, that's the issue. You have explained it better to the devs then I did. See #26683. All the workarounds here will probably work but the database have an 'auto incrementing' key, in this case the incrementing key is 'gen_random_uuid()'. When leaving $incrementing as true will cause errors as explained in #26683 I'm not sure if github can merge tickets, but otherwise this can be closed. |
Closing this in favor of the other issue |
Description:
We use uuid as primary key on our models and have $keyType set to 'string' and $incrementing to false. When we try to create the model (using static function create) it fails to set the id on the model. I can confirm that the role is created in the database.
Steps To Reproduce:
The class in question:
The text was updated successfully, but these errors were encountered: