Use UUID for all of the tables, but assignRole to fail. #2362
-
I'm using Laravel v10.2 and permission v5.91. When I assign a role to a user, I got an error as follows: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`xuehai_databases`.`model_has_roles`, CONSTRAINT `model_has_roles_role_id_foreign` FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`) ON DELETE CASCADE) (Connection: mysql, SQL: insert into `model_has_roles` (`department_id`, `model_type`, `model_uuid`, `role_id`) values (98b9bbc3-0075-4917-be28-11901a788376, App\Models\User, 98b9bbc0-9865-407e-85b9-443653ff3404, 98)) It seems My assign code is as follows: $superUser = User::where("email", "xxx@xxx.com")->first();
$superUser->assignRole("super admin"); // The "super admin" role is already inserted into the role table. Migrate is as follows: Schema::create($tableNames["permissions"], function (Blueprint $table) {
// $table->bigIncrements("id"); // permission id
$table->uuid("id")->primary(); // permission id change to uuid
……
Schema::create($tableNames["roles"], function (Blueprint $table) use (
$teams,
$columnNames
) {
// $table->bigIncrements("id"); // role id
$table->uuid("id")->primary(); // role id change to uuid My config is as follows:
My extended model is as follows: class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable, HasUuids, HasRoles; class Role extends SpatieRole
{
use HasUuids;
…… class Permission extends SpatiePermission
{
use HasUuids; class Department extends Model
{
use HasFactory, HasUuids; Do others have similar problems? Someone could tell me how to fix this problem, please? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
You SQL error statement shows the SQL it's trying to run, and it shows that the Is that the actual |
Beta Was this translation helpful? Give feedback.
-
Did you change to UUID pivots on migration? laravel-permission/tests/TestCase.php Lines 162 to 164 in a769221 laravel-permission/tests/TestCase.php Lines 172 to 174 in a769221 You can use custom pivot names laravel-permission/config/permission.php Lines 78 to 79 in a769221 |
Beta Was this translation helpful? Give feedback.
-
Did you set your custom models on config? laravel-permission/config/permission.php Lines 16 to 27 in a769221 |
Beta Was this translation helpful? Give feedback.
Did you set your custom models on config?
laravel-permission/config/permission.php
Lines 16 to 27 in a769221