S.M*_*ian 2 laravel laravel-5.3
当我更改为时,出现此错误bigIncrements:
.`products` (errno: 150 "Foreign key constraint is incorrectly formed")
Run Code Online (Sandbox Code Playgroud)
我的代码:
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;");
}
Run Code Online (Sandbox Code Playgroud)
但是下面的代码可以正常工作;
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;");
}
Run Code Online (Sandbox Code Playgroud)
由于您使用的bigIncrements()是id,要使其正常运行,请对此进行更改:
$table->integer('parent_product_id')->nullable()->unsigned();
Run Code Online (Sandbox Code Playgroud)
对此:
$table->bigInteger('parent_product_id')->nullable()->unsigned();
Run Code Online (Sandbox Code Playgroud)