簡體   English   中英

運行php artisan migrate時Laravel不兼容的外鍵錯誤

[英]Laravel incompatible foreign key error when running php artisan migrate

我試圖在 laravel 遷移中創建一個外鍵,它引用同一個表上的一列。 它是這樣的

$table->bigInteger('category_id');
$table->unsignedBigInteger('parent_id')->nullable();
$table->foreign('parent_id')->references('category_id')->on('categories')->onDelete('set null')->onUpdate('cascade');

當我運行php artisan migrate:fresh它返回以下錯誤

SQLSTATE[HY000]: General error: 3780 Referencing column 'parent_id' and referenced column 'category_id' in foreign key constraint 'categories_parent_id_foreign' are incompatible.

從我在遷移中寫的內容來看似乎是正確的,但不確定是什么導致了這種情況。

bigIngegerunsignedBigInteger不兼容。

您必須使用相同的類型。

只需用bigInteger替換unsingedBigInteger

$table->unsignedBigInteger('category_id');

...並為您的category_id字段添加索引。

$table->index('category_id');

我希望下面的代碼會起作用。


$table->bigIncrements('category_id');   
$table->bigInteger('parent_id')->unsigned()->nullable();

$table->foreign('parent_id')->references('category_id')->on('categories')->onDelete('set null')->onUpdate('cascade');

暫無
暫無

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

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