【问题标题】:Laravel - errno: 150 "Foreign key constraint is incorrectly formed in migrationLaravel - errno:150“外键约束在迁移中形成错误
【发布时间】:2021-07-27 13:08:16
【问题描述】:

在我的 Laravel-8 应用程序中,我进行了以下迁移:

public function up()
{
    Schema::create('profiles', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->bigInteger('user_id');
        $table->string('first_name',50);
        $table->string('last_name',50);
        $table->string('other_name',50)->nullable();
        $table->string('gender',20);
        $table->string('user_photo',350)->nullable();
        $table->integer('nationality_id');
        $table->string('marital_status',50);
        $table->date('dob')->nullable();
        $table->string('address', 300)->nullable();
        $table->integer('country_id')->nullable();
        $table->integer('state_id')->nullable();
        $table->integer('city_id')->nullable();
        $table->string('cv_file',350)->nullable();
        $table->text('summary')->nullable();
        $table->timestamps();

        $table->foreign('user_id')->references('id')->on('users');
        $table->foreign('nationality_id')->references('id')->on('countries');
        $table->foreign('country_id')->references('id')->on('countries');
        $table->foreign('state_id')->references('id')->on('state_origins');
        $table->foreign('city_id')->references('id')->on('cities');
    });
}

当我做 php artisan migrate 时,我得到了这个错误:

SQLSTATE[HY000]: 一般错误: 1005 Can't create table hrm.profiles (errno: 150 "外键约束格式不正确") (SQL: alter table profiles add constraint profiles_user_id_foreign外键(user_id)引用users(id))

这是用户表:

    Schema::create('users', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name', 100)->nullable();
        $table->string('email', 80)->nullable();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->string('avatar', 200)->nullable();
        $table->boolean('active')->default(false);
        $table->string('activation_token', 250)->nullable();
        $table->rememberToken();
        $table->timestamps();
        $table->softDeletes()->nullable();
        $table->timestamp('last_login_at')->nullable();
    });
}

我该如何解决这个问题?

谢谢

【问题讨论】:

  • 所有外键都需要无符号,将bigInteger更改为unsignedBigInteger,primary和foreign references必须是同一类型。 bigIncrements() 需要 unsignedBigInteger() 和 increments() 需要 unsignedInteger()
  • 嗯,我认为你的 mysql 服务器有问题。我在我的 wampserver 中运行了您的代码,并且创建的表没有任何错误。

标签: laravel


【解决方案1】:

您所有的外键都需要未签名,将bigInteger更改为unsignedBigInteger

primary 和 foreign 引用必须属于同一类型。 bigIncrements() 需要 unsignedBigInteger() 和 increments() 需要 unsignedInteger()

【讨论】:

    【解决方案2】:

    您需要运行在表中用作外键表的所有迁移。就像,您在profile 表中将user_id 定义为外部,那么您必须在profile 迁移文件之前运行user 迁移。对于所有其他人来说,这将是相同的过程。并确保外键是用unsignedBigInteger() 定义的

    $table->unsignedBigInteger('user_id')

    【讨论】:

      猜你喜欢
      • 2021-12-24
      • 2021-07-22
      • 2018-05-23
      • 2015-12-16
      • 2019-09-10
      • 1970-01-01
      • 1970-01-01
      • 2020-12-23
      • 2020-04-04
      相关资源
      最近更新 更多