A
Пытаюсь сделать колонке
parent_id
связь foreign с той же таблицой.Миграция:
Schema::create('main_product_categories', function (Blueprint $table) {
$table->id();
$table->string('slug')->unique()->nullable()->index();
$table->string("title")->unique();
$table->unsignedBigInteger('parent_id')->default(0);
//
// $table->unsignedBigInteger('parent_id')->nullable();
//
$table->foreign('parent_id')->references('id')->on('main_product_categories')
->onDelete('cascade');
$table->timestamps();
$table->softDeletes();
});
Если добавить полю nullable, выдает тоже эту ошибку
Когда хочу создать новую категорию выдает ошибку:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`laravel`.`di3o1s19_main_product_categories`, CONSTRAINT
di3o1s19_main_product_categories_parent_id_foreign
FOREIGN KEY (`parent_id`) REFERENCES di3o1s19_main_product_categories
(`id`) ON ) (SQL: insert into di3o1s19_main_product_categories
(`title`, parent_id
, slug
, updated_at
, `created_at`) values (Автомобильные запчасти, 0, avtomobilnye-zapchasti, 2021-07-03 00:27:51, 2021-07-03 00:27:51))