迁移表时出现问题。这个问题在下面给出

时间:2019-09-30 18:26:07

标签: php laravel

此类问题已在stackoverflow中提出。有一些解决方案,但是我有同样的问题,我无法解决。

public function up()
{
    Schema::create('items', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->integer('category_id')->unsigned();
        $table->string('name');
        $table->text('description');
        $table->integer('price');
        $table->string('image');
        $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
        $table->timestamps();
    });

}

我在尝试迁移时收到此错误:

  

Illuminate \ Database \ QueryException:SQLSTATE [HY000]:常规错误:   1005无法创建表project_rms#sql-4448_15f(错误号:150   “外键约束格式不正确”)(SQL:alter table   items添加约束items_category_id_foreign外键   (category_id)在删除级联上引用了categoriesid

请帮助我找到解决方案,谢谢。

2 个答案:

答案 0 :(得分:0)

外键约束格式不正确 我想您是从这里找到解决方案的。enter link description here

答案 1 :(得分:0)

由于主键是无符号大整数,所以外键也需要是无符号大整数。

这是您的代码的有效示例:

public function up()
{
    Schema::create('items', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->unsignedBigInteger('category_id'); //this line is changed
        $table->string('name');
        $table->text('description');
        $table->integer('price');
        $table->string('image');
        $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
        $table->timestamps();
    });

}