【问题标题】:Laravel 5.8 foreign key migration error - "Foreign key constraint is incorrectly formed"Laravel 5.8 外键迁移错误 - “外键约束格式不正确”
【发布时间】:2020-11-12 03:12:21
【问题描述】:

我需要创建外键来将表书与表用户连接起来。我知道 user_id 列必须是相同的类型,如果 origin col 是无符号的,但它应该没问题。两个表都是 bigint(20) 无符号的。但我收到一个错误:“外键约束格式不正确

这里是迁移代码:

public function up()
{
    Schema::table('books', function (Blueprint $table) {
        $table->unsignedBigInteger('user_id')->nullable()->index()->after('id');

        $table->foreign('user_id')->references('id')->on('users')->onDelete('SET NULL')->onUpdate('CASCADE');
    });
}

这里是用户表迁移

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
}

谁能告诉我问题出在哪里?非常感谢。

【问题讨论】:

    标签: foreign-keys migration laravel-5.8


    【解决方案1】:

    该问题是由表的默认 MariaDB MyIsam 引擎引起的。所以我需要使用默认为 InnoDB 的新 my.ini 重新启动 db,然后再次迁移所有表。

    【讨论】:

      猜你喜欢
      • 2021-12-24
      • 2021-04-06
      • 2017-11-12
      • 2019-07-10
      • 2019-07-27
      • 2018-05-23
      • 2015-12-16
      • 2019-09-10
      • 2022-11-27
      相关资源
      最近更新 更多