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

Implementing UUIDs as a Primary Key has an Issue on Assigning User to a Certain Role #1584

Closed
gieart87 opened this issue Oct 7, 2020 · 2 comments · Fixed by #2089
Closed

Comments

@gieart87
Copy link

gieart87 commented Oct 7, 2020

Hi,
I've just tried to integrate this package into the Laravel 8 project using UUIDs implementation as a Primary Key. Everything looks fine until I try to assign a user to a certain role, e.g 'Admin'. My code:

$user = $request->user(); $assignUserToRole = $user->assignRole('Admin');

and I got the following error:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (laravel8_bootstrapdb.model_has_roles, CONSTRAINT model_has_roles_role_id_foreign FOREIGN KEY (role_id) REFERENCES roles (id) ON DELETE CASCADE) (SQL: insert into model_has_roles (model_id, model_type, role_id) values (91b57341-5e99-4057-9b94-826a76579d16, App\Models\User, 91))

My own Role model that extends the Spatie's Role model:
`<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Str;

use App\Models\Concerns\UsesUuid;

class Role extends \Spatie\Permission\Models\Role
{
use HasFactory, UsesUuid;

protected $fillable = [
    'id',
    'name',
    'guard_name',
];

protected $primaryKey = 'id';
public $incrementing = false;

protected $casts = [
    'id' => 'string'
];

}
`

It looks like the String casting doesn't work like expected. Because the role id still an integer value. And when I tried to implement the String casting in the original Spatie's Role model, everything works fine then. But, I wonder why it works fine when assigning permission to a user which is I thought has a similar case.

So, if you guys know how to solve this issue without touching the original package please let me know. Thanks.

@gieart87 gieart87 closed this as completed Oct 7, 2020
@drbyte
Copy link
Collaborator

drbyte commented Oct 7, 2020

@rezadaulay
Copy link

Please check my comment here: #1639 (comment)

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