【问题标题】:Laravel : Can't add foreign key constraintLaravel:无法添加外键约束
【发布时间】:2020-09-12 14:18:49
【问题描述】:

我想将外键从 order_product_table 上的“order_id”添加到 order_table 上的“id”。

我无法迁移这两个表,出现错误:

General error: 1215 Cannot add foreign key constraint (SQL: alter table `order_products` add constraint `order_products_order_id_foreign` foreign key (`order_id`) references `orders` (`id`) on delete set null on update cascade)

2020_05_14_135759_create_orders_table

        Schema::create('orders', function (Blueprint $table) {
            $table->increments('id');

            $table->integer('user_id')->unsigned()->nullable();
            $table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('set null');

            $table->string('billing_email');
            $table->string('billing_name');
            $table->string('billing_address');
            $table->string('billing_city');
            $table->string('billing_postalcode');
            $table->string('billing_phone');
            $table->string('billing_name_on_card');
            $table->integer('billing_total');
            $table->timestamps();
        });

2020_05_14_142218_create_order_products_table

Schema::create('order_products', function (Blueprint $table) {
            $table->increments('id');

            $table->integer('order_id')->unsigned()->nullable();
            $table->foreign('order_id')->references('id')->on('orders')->onUpdate('cascade')->onDelete('set null');

            $table->integer('product_id')->unsigned()->nullable();
            $table->integer('quantity')->unsigned();
            $table->timestamps()

感谢您的帮助。

【问题讨论】:

    标签: database laravel join foreign-keys migrate


    【解决方案1】:

    尝试更改:$table->increments('id');到 $table->bigIncrements('id')

    【讨论】:

    • 尝试更改:$table->increments('id');到 $table->bigIncrements('id')
    猜你喜欢
    • 2019-10-26
    • 1970-01-01
    • 2021-10-27
    • 2018-11-25
    • 2016-11-11
    • 2017-09-06
    • 2017-03-03
    • 1970-01-01
    • 2022-11-17
    相关资源
    最近更新 更多