【问题标题】:Laravel 8 Migration "General error: 1215 Cannot add foreign key constraint"Laravel 8 迁移“一般错误:1215 无法添加外键约束”
【发布时间】:2021-06-18 13:04:46
【问题描述】:

我正在尝试在 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_orderadd constraintproduct_variation_order_order_id_foreign foreign key (order_id) references orders (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类型是整数吗?
  • 您可以发布您的product_variations 迁移吗?外键和它们引用的列必须是完全相同的类型
  • @brombeer 是的,只是在下面添加了它
  • 抱歉,请迁移您的orders 表。尝试创建外键时,ordersproduct_variations 表是否存在?
  • @brombeer 我可以留给你我的 github 仓库:github.com/rust-7/fore-api 我所有的迁移都在 app > database > migrations,我认为这样更好,因为迁移太多了

标签: php laravel migration database-migration laravel-8


【解决方案1】:

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

在您的订单表中,您将 id 定义为 bigIncrements(),它是无符号大整数。

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

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

所以把order_id改成无符号大整数。

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

同样为了一致性,所有的key都应该是bigIncrements()或者unsignedBigInteger(),这样以后你就不会头疼了。

【讨论】:

    【解决方案2】:

    使用此代码:

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

    }

    【讨论】:

    • OP为什么要“使用这个代码”?出了什么问题?你做了什么改变使它工作?在你的回答中稍微解释一下会很好
    • 我认为 index() 有问题,他在迁移中使用。
    猜你喜欢
    • 2021-03-29
    • 2020-07-08
    • 2019-07-27
    • 2019-03-02
    • 2020-06-12
    • 2021-09-28
    • 2019-12-27
    • 1970-01-01
    • 2021-08-26
    相关资源
    最近更新 更多