Laravel 如何解决 SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1-1' for key 'PRIMARY' 错误
Laravel How to solve SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1-1' for key 'PRIMARY' error
我的 laravel
应用程序中有一个播种器,我尝试执行以下操作:
DB::table('users_roles')->insert([
'user_id' => 1,
'role_id' => 1,
]);
DB::table('users_roles')->insert([
'user_id' => 2,
'role_id' => 1,
]);
DB::table('users_roles')->insert([
'user_id' => 3,
'role_id' => 3,
]);
etc. etc...
当我尝试 运行 这个播种器时,我收到错误:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1-1' for key 'PRIMARY' ....
这里是table的迁移:
Schema::create('users_roles', function (Blueprint $table) {
$table->unsignedInteger('user_id');
$table->unsignedInteger('role_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
$table->primary(['user_id','role_id']);
});
我该如何解决这个问题?
如果 role_id 是主要的,那么问题出在您的播种数据上。这意味着您的 table 架构不兼容。您应该添加另一列作为主键并删除 role_id
上的主键约束
检查你的数据库。 role_id
字段可以设置为 unique/unsigned
属性。
role_id
的 unsigned/unique
您应该删除它或 select none 属性。
The PRIMARY KEY constraint uniquely identifies each record in a table.
Primary keys must contain UNIQUE values, and cannot contain NULL
values.
A table can have only ONE primary key; and in the table, this primary
key can consist of single or multiple columns (fields).
您定义了 2 个主键:
$table->primary(['user_id','role_id']);
将迁移更改为:
Schema::create('users_roles', function (Blueprint $table) {
$table->bigIncrements('id'); // id will be your primary key
$table->unsignedInteger('user_id');
$table->unsignedInteger('role_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
});
我的 laravel
应用程序中有一个播种器,我尝试执行以下操作:
DB::table('users_roles')->insert([
'user_id' => 1,
'role_id' => 1,
]);
DB::table('users_roles')->insert([
'user_id' => 2,
'role_id' => 1,
]);
DB::table('users_roles')->insert([
'user_id' => 3,
'role_id' => 3,
]);
etc. etc...
当我尝试 运行 这个播种器时,我收到错误:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1-1' for key 'PRIMARY' ....
这里是table的迁移:
Schema::create('users_roles', function (Blueprint $table) {
$table->unsignedInteger('user_id');
$table->unsignedInteger('role_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
$table->primary(['user_id','role_id']);
});
我该如何解决这个问题?
如果 role_id 是主要的,那么问题出在您的播种数据上。这意味着您的 table 架构不兼容。您应该添加另一列作为主键并删除 role_id
上的主键约束检查你的数据库。 role_id
字段可以设置为 unique/unsigned
属性。
role_id
的 unsigned/unique
您应该删除它或 select none 属性。
The PRIMARY KEY constraint uniquely identifies each record in a table.
Primary keys must contain UNIQUE values, and cannot contain NULL values.
A table can have only ONE primary key; and in the table, this primary key can consist of single or multiple columns (fields).
您定义了 2 个主键:
$table->primary(['user_id','role_id']);
将迁移更改为:
Schema::create('users_roles', function (Blueprint $table) {
$table->bigIncrements('id'); // id will be your primary key
$table->unsignedInteger('user_id');
$table->unsignedInteger('role_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
});