SQLSTATE[HY000]:一般错误:1005 无法创建表`test`.`members`(错误号:150“外键约束的格式不正确”)

时间:2021-01-20 05:39:59

标签: mysql database laravel eloquent database-migration

我在 Laravel 中使用我的迁移来创建表之间的关系,我有 4 个表:users、members、member_skills 和 Skills。我有以下用户表代码:

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');
    });
}

成员表:

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');
    });
}

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');
    });
}

和技能表:

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');
    });
}

但是,运行我的迁移结果到 (errno: 150 "Foreign key constraint is incorrectly formed")。我看过更改迁移顺序应该可以解决问题,所以我按照用户、成员、成员技能和技能的顺序排列了要迁移的4个表,但我仍然收到相同的错误。我还有什么地方做错了吗?

2 个答案:

答案 0 :(得分:1)

这是正确的方法

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');
   });
}

更多:https://laravel.com/docs/8.x/migrations#foreign-key-constraints

答案 1 :(得分:1)

您应该尝试使用成员表的 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');
        });
    }

您收到此错误是因为您试图在成员表中引用 name,而该成员表已经是用户表的外键。

您可以通过刀片中的 id 外键访问成员的姓名