首頁  >  文章  >  php框架  >  匯總laravel migrate的一些常見錯誤

匯總laravel migrate的一些常見錯誤

藏色散人
藏色散人轉載
2022-01-05 16:07:262252瀏覽

以下由Laravel教學專欄為大家介紹關於laravel migrate初學的一些常見錯誤及其解決辦法,希望對大家有所幫助!

前言

最近斷斷續續開始laravel 入門學習,想整個簡單的通訊錄系統,設立了兩個表,一個branches ,一個contacts。在建立migration 檔案的時候,沒有考慮仔細,先把contacts 表建立了,contacts 表有個外鍵連接到branches 的id,結果執行migrate 指令的時候,出現以下錯誤:

[Illuminate\Database\QueryException] 
 
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `contacts` add constraint `contac 
 
ts_branch_id_foreign` foreign key (`branch_id`) references `branches` (`id`) on delete cascade) 
 
[PDOException] 
 
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint

初步懷疑是表格建立先後不規範造成,於是,手動修改branches 的migration 檔案名稱上的日期,再執行

php artisan migrate:reset

出現如下錯誤:

[ErrorException] 
 
include(/Users/Ade/www/laravel_phonebook5.2): failed to open stream: Operation now in progress

#failed to open stream 錯誤解決

光看錯誤提示不是很理解,我們查看laravel 的log 檔案

more storage/logs/laravel.log

找到出現ERROR 的那段話:

[2016-09-29 18:05:35] local.ERROR: exception 'ErrorException' with message 'include(/Users/Ade/www/laravel_phonebook5.2): failed to open stream: Operation now in progress' in /Users/Ade/www/laravel_phonebook5.2/vendor/composer/ClassLoader.php:412 
Stack trace: 
#0 /Users/Ade/www/laravel_phonebook5.2/vendor/composer/ClassLoader.php(412): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(2, 'include(/Users/...', '/Users/Ade/www/...', 412, Array) 
#1 /Users/Ade/www/laravel_phonebook5.2/vendor/composer/ClassLoader.php(412): Composer\Autoload\includeFile() 
#2 /Users/Ade/www/laravel_phonebook5.2/vendor/composer/ClassLoader.php(301): Composer\Autoload\includeFile('/Users/Ade/www/...') 
#3 [internal function]: Composer\Autoload\ClassLoader->loadClass('CreateBranchesT...') 
#4 /Users/Ade/www/laravel_phonebook5.2/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php(335): spl_autoload_call('CreateBranchesT...') 
#5 /Users/Ade/www/laravel_phonebook5.2/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php(227): Illuminate\Database\Migrations\Migrator->resolve('2016_09_12_1728...') 
#6 /Users/Ade/www/laravel_phonebook5.2/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php(206): Illuminate\Database\Migrations\Migrator->runDown(Object(stdClass), false)

錯誤出現在ClassLoader.php 檔案的412 行

查看改行程式碼,發現是一個呼叫檔案的語句:

匯總laravel migrate的一些常見錯誤

而這個文件,在log 文件中已經指出,即resolve('2016_09_12_1728...') 。 log 提示的這個名稱,就是我修改的 branch 的 migration 檔案名稱。

我們再搜搜正常的migration 檔案會在哪些地方出現:

mdfind 2014_10_12_000000_create_users_table.php|grep phonebook

匯總laravel migrate的一些常見錯誤

可見,正常的有3 個地方出現,修改過的只有1 個地方出現。

編輯這兩個未出現的檔案

#調整autoload_static.php 檔案



匯總laravel migrate的一些常見錯誤

#發現vendor/composer/autoload_static.php 檔案中,和branches 相關的語句如下:

'CreateBranchesTable' => __DIR__ .,

想來應該是改名的時候,PHP Storm自動幫我把這個檔案裡面有關branches 檔案路徑全部給刪掉了。加回去就好了。

參考正常的migration 檔案名稱的設定情況,補充為

'CreateBranchesTable' => __DIR__ . '/../..' . '/database/migrations/2016_09_12_172822_create_branches_table.php',

#調整autoload_classmap.php 檔案

我們發現autoload_classmap.php 檔案中,有關branches 的路徑名稱還是修改前的路徑:匯總laravel migrate的一些常見錯誤

'CreateBranchesTable' => $baseDir . '/database/migrations/2016_09_29_172822_create_branches_table.php',

將其修改為

'CreateBranchesTable' => $baseDir . '/database/migrations/2016_09_12_172822_create_branches_table.php',

再執行migrate 指令

php artisan migrate:reset

匯總laravel migrate的一些常見錯誤

#OK,剛才的錯誤沒了,不過我們又發現contacts 表沒有回滾,


contacts 回滾失敗的分析

匯總laravel migrate的一些常見錯誤


####################################################################################################################### #########透過sequel pro 連上資料庫查看################發現contacts 表果然存在,但是migration 表中已沒有內容,想必再執行前面migrate指令的時候出現錯誤,contacts 的執行記錄並沒有寫入migrations 表中。我們可以重新執行 migrate 指令試試看。首先手動刪除這兩張表,也就是清空資料庫,然後執行:###
php artisan migrate
###我們先忽略創建contacts 表出現的錯誤,刷新sequel pro 檢視一下:############################################################## ####果然,migration 表中沒有contacts 的建立記錄,這也就難怪執行reset 的時候,會沒有contacts 的回滾操作了。 ###############contacts 無法建立branch_id 外鍵的解決###############現在,我們已經執行了migrate 指令,我們重新來看看這個最早出現的錯誤:###
[Illuminate\Database\QueryException] 
 
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `contacts` add constraint `contacts_branch_id_foreign` foreign key (`branch_id`) references `br 
 
anches` (`id`) on update cascade) 
 
[PDOException] 
 
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
###冷靜下來分析一下,既然提示的是SQL 錯誤,我們不妨在sequel pro 中手工執行一下這條SQL 語句。 ###############果然,執行回傳錯誤。 #########仔細查看語句並沒有錯誤,一想,應該是 branch_id 類型宣告和 branches 表中的 ID 類型不一致造成的吧。檢視 contacts 的結構,發現 Unsigned 沒有打鉤,勾選後再執行增加外鍵的 SQL 語句,成功。 ###

匯總laravel migrate的一些常見錯誤

找到问题原因后,我们就清空数据库,修改 contacts 的 migration 文件,调整 branch_id 为:

$table->integer('branch_id')->unsigned()->comment('机构ID');

再重新执行 migrate 命令,成功!

匯總laravel migrate的一些常見錯誤

相关推荐:最新的五个Laravel视频教程

以上是匯總laravel migrate的一些常見錯誤的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:jb51.net。如有侵權,請聯絡admin@php.cn刪除