【问题标题】:Laravel migration (errno: 150 “Foreign key constraint is incorrectly formed”)Laravel 迁移 (errno: 150 “外键约束格式不正确”)
【发布时间】:2019-09-10 12:32:54
【问题描述】:

当用户单击链接时,我试图通过使用列 chatter_discussion 从数据库中获取特定数据,但出现此错误:

SQLSTATE[HY000]:一般错误:1005 无法创建表 forums.chatter_discussion (errno: 150 "外键约束是 格式不正确”) (SQL: alter table chatter_discussion add 约束chatter_discussion_user_id_foreign外键 (user_id) 引用users (id) 在更新时删除级联 级联)

Schema::table('chatter_discussion', function (Blueprint $table) {
    $table->foreign('chatter_category_id')->references('id')->on('chatter_categories')
        ->onDelete('cascade')
        ->onUpdate('cascade');
    $table->foreign('user_id')->references('id')->on('users')
        ->onDelete('cascade')
        ->onUpdate('cascade');
});

Schema::table('chatter_post', function (Blueprint $table) {
    $table->foreign('chatter_discussion_id')->references('id')->on('chatter_discussion')
        ->onDelete('cascade')
        ->onUpdate('cascade');
    $table->foreign('user_id')->references('id')->on('users')
        ->onDelete('cascade')
        ->onUpdate('cascade');
    });
}

【问题讨论】:

  • 你可以通过发布users表的定义来帮助别人帮助你。

标签: php html laravel-5


【解决方案1】:
public function up()
{
    Schema::create('companies', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->text('address');
        $table->string('tel1');
        $table->string('tel2');
        $table->integer('owner');
        $table->unsignedBigInteger('access_id');
        $table->string('depot_number')->default(2);
        $table->timestamps();
        $table->foreign('access_id')->references('id')->on('accesses')->onDelete('cascade');
    });
}

public function up()
{
    Schema::create('accesses', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('type');
        $table->string('description');
        $table->timestamps();
    });
}

在您的 database/migrations 文件夹中,按名称排序。然后确保 create_accesses_tablecreate_companies_table 之前 enter image description here

【讨论】:

    【解决方案2】:

    当迁移到新版本的 Laravel 时,通常会发生此问题,在架构中定义主键时使用 bigIncrements 而不是 increments。 另外,在定义外键关系之前,还需要先定义外键的类型。

    解决方法是定义外键的类型,然后定义外键关系,例如:

    Schema::table('chatter_discussion', function (Blueprint $table) {
    // first define the type of the foreign keys in the schema
    $table->bigInteger('chatter_category_id')->unsigned(); // the id of the chatter category
    $table->bigInteger('user_id')->unsigned(); // the id of the user
    /*
    or use:
    $table->integer('chatter_category_id');
    $table->integer('user_id');
    if using older versions of laravel, whatever works
    */        
    // THEN define foreign key relations
    $table->foreign('chatter_category_id')->references('id')->on('chatter_categories')
                        ->onDelete('cascade')
                        ->onUpdate('cascade');
            $table->foreign('user_id')->references('id')->on('users')
                        ->onDelete('cascade')
                        ->onUpdate('cascade');
        });
    

    对引用外键的其他表执行类似操作

    注意在您的示例中,您没有在 chatter_discussion 表中定义 id 列,然后在 chatter_post 中引用该列。不确定您是否错过了它,或者它是在之前的迁移中定义的。

    【讨论】:

    • 谢谢,但我已经在两个表中定义了 chatter_category_id 和 user_id,它们已成功迁移
    • @AbdessatarGhoudi,如果您已经定义了外键列,那么列的数据类型可能不匹配(例如,使用 bigIncrements 而不是 incerements,反之亦然)。大多数情况下,这就是问题所在。您可以编辑您的问题并添加这些迁移,以便我知道出了什么问题并做出相应的回复
    猜你喜欢
    • 2018-05-23
    • 2015-12-16
    • 1970-01-01
    • 2017-04-13
    • 2019-09-17
    • 2020-12-16
    • 1970-01-01
    • 2019-07-27
    相关资源
    最近更新 更多