繁体   English   中英

Laravel 8 迁移“一般错误:1215 无法添加外键约束”

[英]Laravel 8 Migration “General error: 1215 Cannot add foreign key constraint”

我正在尝试在 Laravel 8 上创建迁移,这是我的表

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_order add constraint product_variation_order_order_id_foreign foreign key ( order_id ) on delete cascade) ) 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');
    }
}

外键必须与引用键的类型相同。

在您的订单表中,您将 id 定义为bigIncrements() ,它是 unsigned big integer。

在您的 product_variation_order 表中,您将 order_id 定义为无符号 integer。

所以这两个键不匹配。 通常你应该使用大 integer 因为它允许你的数据库变得更大并且 integer 和大 integer 之间的空间差异并不显着。

所以将order_id更改为 unsigned big integer。

$table->unsignedBigInteger('order_id')->nullable();

同样为了一致性,所有的键都应该是bigIncrements()unsignedBigInteger() ,这会让你以后头疼。

使用此代码:

产品变体:

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');
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM