Laravel 8 Migration "General error: 1215 Cannot add foreign key constraint"
Laravel 8 Migration "General error: 1215 Cannot add foreign key constraint"
我正在尝试在 Laravel 8 上创建迁移,这是我的 table
class CreateProductVariationOrderTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('product_variation_order', function (Blueprint $table) {
$table->integer('order_id')->unsigned()->index();
$table->integer('product_variation_id')->unsigned()->index();
$table->integer('quantity')->unsigned();
$table->timestamps();
$table->foreign('order_id')->references('id')->on('orders')->onDelete('cascade');
$table->foreign('product_variation_id')->references('id')->on('product_variations')->onDelete('cascade');
});
}
public function down()
{
Schema::dropIfExists('product_variation_order');
}
}
当我 运行 php artisan migrate
时,我得到了一大块错误:SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table
product_variation_orderadd constraint
product_variation_order_order_id_foreign foreign key (
order_id) references
订单 (
id) on delete cascade)
⚠️ 编辑:这是我的 product_variation
迁移文件。
class CreateProductVariationsTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('product_variations', function (Blueprint $table) {
$table->increments('id');
$table->integer('product_id')->unsigned()->index();
$table->string('name');
$table->integer('price')->nullable();
$table->integer('order')->nullable();
$table->timestamps();
$table->foreign('product_id')->references('id')->on('products');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('product_variations');
}
}
使用此代码:
product_variations:
class CreateProductVariationsTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('product_variations', function (Blueprint $table) {
$table->increments('id');
$table->bigInteger('product_id')->unsigned()->nullable();
$table->string('name');
$table->integer('price')->nullable();
$table->integer('order')->nullable();
$table->timestamps();
$table->foreign('product_id')->references('id')->on('products');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('product_variations');
}
}
product_variation_order:
class CreateProductVariationOrderTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('product_variation_order', function (Blueprint $table) {
$table->bigInteger('order_id')->unsigned()->nullable();
$table->bigInteger('product_variation_id')->unsigned()->nullable();
$table->integer('quantity')->unsigned();
$table->timestamps();
$table->foreign('order_id')->references('id')->on('orders')->onDelete('cascade');
$table->foreign('product_variation_id')->references('id')->on('product_variations')->onDelete('cascade');
});
}
public function down()
{
Schema::dropIfExists('product_variation_order');
}
}
外键必须与引用键的类型相同。
在您的订单 table 中,您将 id 定义为 bigIncrements()
无符号大整数。
在您的 product_variation_order table 中,您将 order_id 定义为无符号整数。
所以这两个键不匹配。通常你应该使用大整数,因为它允许你的数据库变得更大,而且 space 整数和大整数之间的差异并不显着。
因此将 order_id
更改为无符号大整数。
$table->unsignedBigInteger('order_id')->nullable();
另外为了保持一致性,所有的键都应该是bigIncrements()
或unsignedBigInteger()
,这样以后你就不会头疼了。
我正在尝试在 Laravel 8 上创建迁移,这是我的 table
class CreateProductVariationOrderTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('product_variation_order', function (Blueprint $table) {
$table->integer('order_id')->unsigned()->index();
$table->integer('product_variation_id')->unsigned()->index();
$table->integer('quantity')->unsigned();
$table->timestamps();
$table->foreign('order_id')->references('id')->on('orders')->onDelete('cascade');
$table->foreign('product_variation_id')->references('id')->on('product_variations')->onDelete('cascade');
});
}
public function down()
{
Schema::dropIfExists('product_variation_order');
}
}
当我 运行 php artisan migrate
时,我得到了一大块错误:SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table
product_variation_orderadd constraint
product_variation_order_order_id_foreign foreign key (
order_id) references
订单 (
id) on delete cascade)
⚠️ 编辑:这是我的 product_variation
迁移文件。
class CreateProductVariationsTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('product_variations', function (Blueprint $table) {
$table->increments('id');
$table->integer('product_id')->unsigned()->index();
$table->string('name');
$table->integer('price')->nullable();
$table->integer('order')->nullable();
$table->timestamps();
$table->foreign('product_id')->references('id')->on('products');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('product_variations');
}
}
使用此代码:
product_variations:
class CreateProductVariationsTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('product_variations', function (Blueprint $table) {
$table->increments('id');
$table->bigInteger('product_id')->unsigned()->nullable();
$table->string('name');
$table->integer('price')->nullable();
$table->integer('order')->nullable();
$table->timestamps();
$table->foreign('product_id')->references('id')->on('products');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('product_variations');
}
}
product_variation_order:
class CreateProductVariationOrderTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('product_variation_order', function (Blueprint $table) {
$table->bigInteger('order_id')->unsigned()->nullable();
$table->bigInteger('product_variation_id')->unsigned()->nullable();
$table->integer('quantity')->unsigned();
$table->timestamps();
$table->foreign('order_id')->references('id')->on('orders')->onDelete('cascade');
$table->foreign('product_variation_id')->references('id')->on('product_variations')->onDelete('cascade');
});
}
public function down()
{
Schema::dropIfExists('product_variation_order');
}
}
外键必须与引用键的类型相同。
在您的订单 table 中,您将 id 定义为 bigIncrements()
无符号大整数。
在您的 product_variation_order table 中,您将 order_id 定义为无符号整数。
所以这两个键不匹配。通常你应该使用大整数,因为它允许你的数据库变得更大,而且 space 整数和大整数之间的差异并不显着。
因此将 order_id
更改为无符号大整数。
$table->unsignedBigInteger('order_id')->nullable();
另外为了保持一致性,所有的键都应该是bigIncrements()
或unsignedBigInteger()
,这样以后你就不会头疼了。