亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Laravel 6:SQLSTATE[HY000]:一般錯誤:1215 無法添加外鍵約束

Laravel 6:SQLSTATE[HY000]:一般錯誤:1215 無法添加外鍵約束

PHP
回首憶惘然 2022-01-08 14:59:22
嘿,我目前正在進行 Laravel 6 數據庫遷移,但是當我執行 php artisan migrate:fresh 時,以下錯誤跳到我身上: SQLSTATE [HY000]:一般錯誤:1215 無法添加外鍵約束(SQL:alter table category_postadd constraint category_post_category_id_foreignforeign key ( category_id) 參考id( categories) 在更新級聯上刪除級聯)我檢查了以下內容:錯別字引用順序錯誤自 Laravel 5.8 起從增量更改為大增量錯誤類型這是我的遷移代碼,希望您能看到我犯的錯誤,因為我找不到它。 // Table for storing Blog Posts        Schema::create('posts', function (Blueprint $table) {            $table->bigIncrements('id');            $table->string('title');            $table->string('slug');            $table->string('read_time');            $table->string('summary');            $table->string('body');            /*$table->timestamps('created_at');            $table->timestamps('updated_at');*/            $table->timestamps();        });        // Table for storing categories        Schema::create('categories', function (Blueprint $table) {          $table->bigIncrements('id');          $table->string('name')->unique();          $table->string('slug')->unique();          $table->string('description');          $table->timestamps();        });        // Table for association of Categories with Blog Posts        Schema::create('category_post', function (Blueprint $table) {          $table->bigInteger('category_id');          $table->bigInteger('post_id');          $table->foreign('category_id')->references('id')->on('categories')            ->onUpdate('cascade')->onDelete('cascade');          $table->foreign('post_id')->references('id')->on('posts')            ->onUpdate('cascade')->onDelete('cascade');          $table->primary(['post_id', 'category_id']);        });        // Table for association of Users with Blog Posts        Schema::create('user_post', function (Blueprint $table) {          $table->bigInteger('user_id');          $table->bigInteger('post_id');        });畢竟,目標是將類別和用戶與帖子相關聯,以便我可以在帖子上添加標簽和用戶。提前感謝您的幫助和好意,我對 Laravel 還很陌生,但我對 PHP 有很好的了解,所以如果您能解釋我做錯了什么,那就太好了 :)
查看完整描述

1 回答

?
一只名叫tom的貓

TA貢獻1906條經驗 獲得超3個贊

根據文檔bigIncrements代表:

自動遞增 UNSIGNED BIGINT(主鍵)等效列。

因此,您的外鍵需要與 ( unsignedBigInteger())匹配。


查看完整回答
反對 回復 2022-01-08
  • 1 回答
  • 0 關注
  • 168 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號