Skip to content
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

Closed
matthijs opened this issue Nov 29, 2018 · 12 comments
Closed

Missing the primary key (id) on the model when using create #26673

matthijs opened this issue Nov 29, 2018 · 12 comments

Comments

@matthijs
Copy link

  • Laravel Version: 5.7.15
  • PHP Version: 7.2
  • Database Driver & Version: PostgreSQL 11

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:

class Role extends Model
{
	use SoftDeletes;

	/**
	 * The "type" of the auto-incrementing ID.
	 *
	 * @var string
	 */
	protected $keyType = 'string';

	/**
	 * Indicates if the IDs are auto-incrementing.
	 *
	 * @var bool
	 */
	public $incrementing = false;

	/**
	 * The attributes that are mass assignable
	 *
	 * @var array
	 */
	protected $fillable = [
		'name', 'display_name', 'description',
	];
}
% ./artisan tinker
Psy Shell v0.9.9 (PHP 7.2.9-1 — cli) by Justin Hileman
>>> use App\Model\Acl\Role;
>>> $x = Role::create(['name' => 'testrole', 'display_name' => 'testrole']);
=> App\Model\Acl\Role {#3349
     name: "testrole",
     display_name: "testrole",
     updated_at: "2018-11-29 19:30:45",
     created_at: "2018-11-29 19:30:45",
   }
>>> $x->id
=> null
@staudenmeir
Copy link
Contributor

What type of column is id? What's the id value in the database?

@sisve
Copy link
Contributor

sisve commented Nov 30, 2018

What's in the database table? Where are the uuids generated?

@matthijs
Copy link
Author

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();
});

@dinhquochan
Copy link
Contributor

dinhquochan commented Nov 30, 2018

You must define value of the id if you don't set incrementing. Or automatic set key in Laravel Model Creating Event:

public static function boot()
{
    parent::boot();

    static::creating(function ($model) {
        $model->setAttribute('id', Str::uuid());
    });
}

@CrixuAMG
Copy link

I agree with @dinhquochan 's solution. However, I would put the logic in a trait, so it can be reused for other models easily.

@dinhquochan
Copy link
Contributor

@matthijs @CrixuAMG something like that spatie/laravel-binary-uuid

@driesvints
Copy link
Member

@matthijs did it work with @dinhquochan's solution?

@matthijs
Copy link
Author

@driesvints I'll test it when back at home.

@CrixuAMG
Copy link

@dinhquochan exactly, but for such a little thing creating your own trait would be better imho.

@zecipriano
Copy link

Until 5.7.15 (just opened an issue about it) you could just leave $incrementing as true and Laravel would get the id from the database after inserting (the same as if it was an int).

@matthijs
Copy link
Author

@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.

@driesvints
Copy link
Member

Closing this in favor of the other issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants