[英]SQLSTATE[HY000]: General error: 1005 Can't create table `test`.`members` (errno: 150 “Foreign key constraint is incorrectly formed”)
I'm using my migrations in Laravel to create the relationships between tables, and I have 4 tables: users, members, member_skills, and skills.我在 Laravel 中使用我的迁移来创建表之间的关系,我有 4 个表:用户、成员、成员技能和技能。 I have the following code for the users table:
我有用户表的以下代码:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
$table->boolean('admin');
});
}
the members table:成员表:
public function up()
{
Schema::create('members', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->string('name');
$table->string('status');
$table->date('date')->nullable();
$table->text('project')->nullable();
$table->date('start')->nullable();
$table->foreign('name')->references('name')->on('users');
});
}
the member_skills table: member_skills 表:
public function up()
{
Schema::create('member_skills', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->string('name');
$table->string('skill');
$table->foreign('name')->references('name')->on('members');
});
}
and the skills table:和技能表:
public function up()
{
Schema::create('skills', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->string('skill');
$table->text('description');
$table->foreign('skill')->references('skill')->on('member_skills');
});
}
However, running my migrations results to (errno: 150 "Foreign key constraint is incorrectly formed")
.但是,运行我的迁移结果为
(errno: 150 "Foreign key constraint is incorrectly formed")
。 I have read that changing the migration order should fix the problem, so I have arranged the 4 tables to be migrated in the order of users, members, member_skills, and skills, but I am still receiving the same error.我已经读过更改迁移顺序应该可以解决问题,所以我按照用户、成员、成员技能和技能的顺序排列了要迁移的 4 个表,但我仍然收到同样的错误。 Is there anything else I'm doing wrong?
还有什么我做错了吗?
Here is the right way todo this这是执行此操作的正确方法
public function up()
{
Schema::create('members', function (Blueprint $table) {
...
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
});
}
public function up()
{
Schema::create('member_skills', function (Blueprint $table) {
...
$table->unsignedBigInteger('member_id');
$table->foreign('member_id')->references('id')->on('members');
});
}
public function up()
{
Schema::create('skills', function (Blueprint $table) {
...
$table->unsignedBigInteger('member_skill_id');
$table->foreign('member_skill_id')->references('id')->on('member_skills');
});
}
more:https://laravel.com/docs/8.x/migrations#foreign-key-constraints更多:https://laravel.com/docs/8.x/migrations#foreign-key-constraints
You should try using the id of the member table as the foreign key rather than the name in the member_skills schema您应该尝试使用成员表的id作为外键,而不是 member_skills 模式中的名称
public function up()
{
Schema::create('member_skills', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->string('member_id');
$table->string('skill');
$table->foreign('member_id')->references('id')->on('members');
});
}
You are getting this error because you are trying to reference name in the member table which is already a foreign key to the users table.您收到此错误是因为您试图在成员表中引用名称,该成员表已经是用户表的外键。
You can access the name of the member through the id foreign key in your blade.您可以通过刀片中的id外键访问成员的名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.