当我将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;");
}
答案 0 :(得分:4)
由于您bigIncrements()
使用了id
,因此要改变它:
$table->integer('parent_product_id')->nullable()->unsigned();
到此:
$table->bigInteger('parent_product_id')->nullable()->unsigned();