簡體   English   中英

Laravel:1215無法添加外鍵約束-遷移時

[英]Laravel: 1215 Cannot add foreign key constraint - when migrating

我有表類別,其本身具有父類型。

Schema::create('categories', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->integer('parent_id')->unsigned()->nullable()->default(null);
        $table->foreign('parent_id')->references('id')->on('categories');
        $table->timestamps();
    });

拋出:

SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `categories` add constraint `categories_parent_id_foreign` foreign key (`parent_id`) references `categories` (`id`))

我已經嘗試過在單獨的Schema :: table函數中將parent_id設置為外部,但沒有成功。 表是InnoDB類型。

嘗試將FK從整數更改為bigInteger:

$table->bigInteger('parent_id')->unsigned()->nullable()->default(null);

要么

$table->unsignedBigInteger('parent_id')->nullable()->default(null);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM