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

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

Laravel 遷移已成功遷移,但未在表中創建外鍵關系

Laravel 遷移已成功遷移,但未在表中創建外鍵關系

PHP
動漫人物 2023-11-03 16:46:26
我的 Laravel 應用程序成功創建了遷移中的所有表,但無法在表中創建外鍵關系,甚至在刪除主記錄時無法強制級聯。這是遷移。    Schema::create('articles', function (Blueprint $table) {        $table->id('id');        $table->unsignedBigInteger('user_id');        $table->string('title');        $table->text('excerpt');        $table->text('body');        $table->timestamps();        $table->foreign('user_id')            ->references('id')            ->on('users')            ->onDelete('cascade');    });當我運行時 php artisan migrate它遷移成功。λ php artisan migrateMigration table created successfully.Migrating: 2014_10_12_000000_create_users_tableMigrated:  2014_10_12_000000_create_users_table (0.11 seconds)Migrating: 2014_10_12_100000_create_password_resets_tableMigrated:  2014_10_12_100000_create_password_resets_table (0.1 seconds)Migrating: 2019_08_19_000000_create_failed_jobs_tableMigrated:  2019_08_19_000000_create_failed_jobs_table (0.07 seconds)Migrating: 2020_08_26_122846_create_articles_tableMigrated:  2020_08_26_122846_create_articles_table (0.14 seconds)但是,當我檢查數據庫時,沒有創建關系,只是創建外鍵索引。 檢查此鏈接中的文章表圖像。我已經標記了必要的部分在此處檢查用戶表圖像。我已經突出顯示了主鍵。我添加了一些與用戶和文章相關的工廠數據,當我刪除用戶時,這些文章將被保留為孤立的??赡艹鍪裁磫栴}了?PHP 版本:7.3.21MySql版本:5.7.31MariaDB 版本:10.4.13Laravel 框架版本:7.25.0先感謝您。
查看完整描述

2 回答

?
德瑪西亞99

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

我所做的就是轉到config文件夾然后database.php將 mysql 數據庫引擎從 null 更改為 innoDB 即來自:


//...

'engine' => null,

//...

到:


//...

'engine' => 'InnoDB',

//...


查看完整回答
反對 回復 2023-11-03
?
慕桂英546537

TA貢獻1848條經驗 獲得超10個贊

您用于Schema::create創建表。


在 Laravel 文檔中,我Schema::table在使用外鍵時看到。也許你應該嘗試拆分你的代碼:


Schema::create('articles', function (Blueprint $table) {

    $table->id('id');

    $table->unsignedBigInteger('user_id');

    $table->string('title');

    $table->text('excerpt');

    $table->text('body');

    $table->timestamps();

});


Schema::table('articles', function (Blueprint $table) {

    $table->foreign('user_id')

        ->references('id')

        ->on('users')

        ->onDelete('cascade');


});


查看完整回答
反對 回復 2023-11-03
  • 2 回答
  • 0 關注
  • 139 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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