“外键约束的格式不正确”(错误号150):Laravel迁移

时间:2020-03-24 13:40:25

标签: laravel

当我迁移数据库时,出现此错误。下面是我的编码:

代码

public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('user_id');
            $table->unsignedBigInteger('category_id')->nullable();
            $table->string('name');
            $table->text('description')->nullable();
            $table->boolean('active')->default(1)->nullable();
            $table->double('code');
            $table->integer('unit')->default(1);
            $table->string('image')->nullable();
            $table->double('price');
            $table->timestamps();

            $table->foreign('user_id')->references('id')->on('users');
            $table->foreign('category_id')->references('id')->on('categories');

        });
    }

错误:

Illuminate \ Database \ QueryException:SQLSTATE [HY000]:常规错误:1005无法创建表reochiproducts(错误号:150“外键约束格式不正确”)(SQL:更改表products添加约束products_category_id_foreign外键(category_id)引用categoriesid))

0 个答案:

没有答案