首页 > 解决方案 > 1215 Impossible d'ajouter des contraintes d'index externe

问题描述

i want to create foreign table and product table keys but it gives me error. 1215 Unable to add external index constraints

(SQL: alter table `category_product` add constraint` category
    _product_category_id_foreign` foreign key (`category_id`) references` category` (`id`) on delete cascade)

i don't know where exactly is the error.

products table

Schema::create('products', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name')->unique();
            $table->string('slug')->unique();
            $table->string('details')->nullable();
            $table->integer('price');
            $table->string('description');
            $table->timestamps();
        });

categories table

Schema::create('categories', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name')->unique();
            $table->string('slug')->unique();
            $table->timestamps();
        });

category_product table

Schema::create('category_product', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('product_id')->unsigned()->nullable();
            $table->foreign('product_id')->references('id')
                  ->on('products')->onDelete('cascade');

            $table->integer('category_id')->unsigned()->nullable();
            $table->foreign('category_id')->references('id')
                  ->on('category')->onDelete('cascade');
            $table->timestamps();
        });

标签: laravel

解决方案


推荐阅读