错误:150"外键约束形成错误" - bigIncrements

时间:2017-05-14 17:26:28

标签: laravel laravel-5.3

当我将I更改为bigIncrements时,我收到此错误:

.`products` (errno: 150 "Foreign key constraint is incorrectly formed")

我的代码:

public function up()
{
    Schema::create('products', function (Blueprint $table) {
        $table->bigIncrements('id')->unsigned();
        $table->string('title');
        $table->integer('current_buy');
        $table->integer('count');
        $table->text('short_description');
        $table->text('long_description');
        $table->tinyInteger('status')->default(0);
        $table->string('series');
        $table->integer('max_buy');
        $table->integer('parent_product_id')->nullable()->unsigned();
        $table->foreign('parent_product_id')->references('id')->on('products')->onDelete('cascade');
        $table->tinyInteger('admin_seen')->default(0);
        $table->timestamps();
    });

    DB::update("ALTER TABLE products AUTO_INCREMENT = 1000;");
}

但是下面的代码工作正常;

public function up()
{
    Schema::create('products', function (Blueprint $table) {
        $table->increments('id');
        $table->string('title');
        $table->integer('current_buy');
        $table->integer('count');
        $table->text('short_description');
        $table->text('long_description');
        $table->tinyInteger('status')->default(0);
        $table->string('series');
        $table->integer('max_buy');
        $table->integer('parent_product_id')->nullable()->unsigned();
        $table->foreign('parent_product_id')->references('id')->on('products')->onDelete('cascade');
        $table->tinyInteger('admin_seen')->default(0);
        $table->timestamps();
    });

   // DB::update("ALTER TABLE products AUTO_INCREMENT = 1000;");
}

1 个答案:

答案 0 :(得分:4)

由于您bigIncrements()使用了id,因此要改变它:

$table->integer('parent_product_id')->nullable()->unsigned();

到此:

$table->bigInteger('parent_product_id')->nullable()->unsigned();