Laravel : Can't add foreign key constraint

Viewed 95

I want to add a foreign key from 'order_id' on the order_product_table to 'id' on the order_table.

I can't migrate these two tables, i'm getting the error :

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()

Thanks for your help.

1 Answers

try change: $table->increments('id'); to $table->bigIncrements('id')

Related