spatie/laravel-permissions assignRole 方法导致错误 [违反完整性约束:1452 无法添加或更新子行]
spatie/laravel-permissions assignRole method causes error [Integrity constraint violation: 1452 Cannot add or update a child row]
我尝试使用播种器为用户分配角色,但我一直收到此错误:
Integrity constraint violation: 1452 Cannot add or update a child row : a foreign key constraint fails (internshipweb
.user_has_roles
, CONSTRAINT user_has_roles_role_id_foreign
FOREIGN KEY (role_id
) REFERENCES roles
(id
) ...
无法弄清楚为什么它不起作用:
这是我的用户模型:
<?php
namespace App\Models\Website;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;
class User extends Authenticatable
{
use Notifiable, HasRoles;
protected $connection = 'mysql2';
protected $guard = 'webuser';
protected $fillable = [
'email', 'password', 'username', 'permissions', 'first_name', 'last_name', 'avatar'
];
protected $hidden = [
'password'
];
}
这是我的播种机:
<?php
use Illuminate\Database\Seeder;
use App\Models\Website\User;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
class WebUserSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Role::create([
'name' => 'admin'
]);
$user = User::create([
'email' => 'admin@gmail.com',
'username' => 'admin',
'password' => bcrypt('password'),
'first_name' => 'Add',
'last_name' => 'Min'
]);
$user->assignRole('admin');
}
}
我试过清除缓存和配置。我似乎找不到有同样问题的人。如果您需要更多信息,请告诉我。
您收到此错误是因为您尝试将行更新为 user_has_roles,但 id[ 没有有效值=25=]字段
基于当前存储在 roles.
中的值
编辑:确保 role_id 作为可填充 属性 包含在您的 user_has_roles 模型中。
这是一项工作 around/hack 但请注意,这将禁用所有外键检查。如果一切都失败了,你可以:
请注意,这是一个临时解决方法,只会持续您当前会话的长度。当您重新连接到数据库时,将重新启用此约束,这是一件好事。
在您的数据库上尝试运行这个查询:
SET FOREIGN_KEY_CHECKS=0
您正在尝试向您的 user_has_roles
table 添加一行,其中 role_id
在您的 roles
table 中不存在。你的 roles
table 播种了吗?在这段代码运行之前,您需要管理员角色存在。如果是后来的播种者,您可以在 RolesTableSeeder
中分配该角色
我尝试使用播种器为用户分配角色,但我一直收到此错误:
Integrity constraint violation: 1452 Cannot add or update a child row : a foreign key constraint fails (
internshipweb
.user_has_roles
, CONSTRAINTuser_has_roles_role_id_foreign
FOREIGN KEY (role_id
) REFERENCESroles
(id
) ...
无法弄清楚为什么它不起作用:
这是我的用户模型:
<?php
namespace App\Models\Website;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;
class User extends Authenticatable
{
use Notifiable, HasRoles;
protected $connection = 'mysql2';
protected $guard = 'webuser';
protected $fillable = [
'email', 'password', 'username', 'permissions', 'first_name', 'last_name', 'avatar'
];
protected $hidden = [
'password'
];
}
这是我的播种机:
<?php
use Illuminate\Database\Seeder;
use App\Models\Website\User;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
class WebUserSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Role::create([
'name' => 'admin'
]);
$user = User::create([
'email' => 'admin@gmail.com',
'username' => 'admin',
'password' => bcrypt('password'),
'first_name' => 'Add',
'last_name' => 'Min'
]);
$user->assignRole('admin');
}
}
我试过清除缓存和配置。我似乎找不到有同样问题的人。如果您需要更多信息,请告诉我。
您收到此错误是因为您尝试将行更新为 user_has_roles,但 id[ 没有有效值=25=]字段 基于当前存储在 roles.
中的值编辑:确保 role_id 作为可填充 属性 包含在您的 user_has_roles 模型中。
这是一项工作 around/hack 但请注意,这将禁用所有外键检查。如果一切都失败了,你可以:
请注意,这是一个临时解决方法,只会持续您当前会话的长度。当您重新连接到数据库时,将重新启用此约束,这是一件好事。
在您的数据库上尝试运行这个查询:
SET FOREIGN_KEY_CHECKS=0
您正在尝试向您的 user_has_roles
table 添加一行,其中 role_id
在您的 roles
table 中不存在。你的 roles
table 播种了吗?在这段代码运行之前,您需要管理员角色存在。如果是后来的播种者,您可以在 RolesTableSeeder