欧美亚洲中文,在线国自产视频,欧洲一区在线观看视频,亚洲综合中文字幕在线观看

      1. <dfn id="rfwes"></dfn>
          <object id="rfwes"></object>
        1. 站長資訊網(wǎng)
          最全最豐富的資訊網(wǎng)站

          匯總laravel migrate的一些常見錯誤

          下面由Laravel教程欄目給大家介紹關(guān)于laravel migrate初學的一些常見錯誤及其解決辦法,希望對大家有所幫助!

          前言

          最近斷斷續(xù)續(xù)開始 laravel 入門學習,想整個簡單的通訊錄系統(tǒng),設立了兩個表,一個 branches ,一個 contacts。在創(chuàng)建 migration 文件的時候,沒有考慮仔細,先把 contacts 表建立了,contacts 表有個外鍵連接到 branches 的 id,結(jié)果執(zhí)行 migrate 命令的時候,出現(xiàn)以下錯誤:

          [IlluminateDatabaseQueryException]    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

          初步懷疑是表創(chuàng)建先后不規(guī)范造成,于是,手動修改 branches 的 migration 文件名稱上的日期,再執(zhí)行

          php artisan migrate:reset

          出現(xiàn)如下錯誤:

          [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

          找到出現(xiàn) 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): IlluminateFoundationBootstrapHandleExceptions->handleError(2, 'include(/Users/...', '/Users/Ade/www/...', 412, Array)  #1 /Users/Ade/www/laravel_phonebook5.2/vendor/composer/ClassLoader.php(412): ComposerAutoloadincludeFile()  #2 /Users/Ade/www/laravel_phonebook5.2/vendor/composer/ClassLoader.php(301): ComposerAutoloadincludeFile('/Users/Ade/www/...')  #3 [internal function]: ComposerAutoloadClassLoader->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): IlluminateDatabaseMigrationsMigrator->resolve('2016_09_12_1728...')  #6 /Users/Ade/www/laravel_phonebook5.2/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php(206): IlluminateDatabaseMigrationsMigrator->runDown(Object(stdClass), false)

          錯誤出現(xiàn)在 ClassLoader.php 文件的 412 行

          查看改行代碼,發(fā)現(xiàn)是一個調(diào)用文件的語句:

          匯總laravel migrate的一些常見錯誤

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

          我們再搜搜正常的 migration 文件會在哪些地方出現(xiàn):

          mdfind 2014_10_12_000000_create_users_table.php|grep phonebook

          匯總laravel migrate的一些常見錯誤

          可見,正常的有 3 個地方出現(xiàn),修改過的只有 1 個地方出現(xiàn)。

          編輯這兩個未出現(xiàn)的文件

          調(diào)整 autoload_static.php 文件

          發(fā)現(xiàn) vendor/composer/autoload_static.php 文件中,和 branches 相關(guān)的語句如下:

          'CreateBranchesTable' => __DIR__ .,

          想來應該是改名的時候,PHP Storm自動幫我把這個文件里面有關(guān) branches 文件路徑全部給刪掉了。加回去就好了。
          參照正常的 migration 文件名的配置情況,補充為

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

          調(diào)整 autoload_classmap.php 文件

          我們發(fā)現(xiàn) autoload_classmap.php 文件中,有關(guān) branches 的路徑名還是修改前的路徑:

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

          將其修改為

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

          再執(zhí)行 migrate 命令

          php artisan migrate:reset

          匯總laravel migrate的一些常見錯誤

          OK,剛才的錯誤沒了,不過我們又發(fā)現(xiàn) contacts 表沒有回滾,

          contacts 回滾失敗的分析

          通過 sequel pro 連上數(shù)據(jù)庫查看

          匯總laravel migrate的一些常見錯誤

          發(fā)現(xiàn) contacts 表果然存在,但是 migration 表中已沒有內(nèi)容,想必再執(zhí)行前面 migrate 命令的時候出現(xiàn)錯誤,contacts 的執(zhí)行記錄并沒有寫入 migrations 表中。我們可以重新執(zhí)行 migrate 命令試試看。首先手動刪除這兩張表,也就是清空數(shù)據(jù)庫,然后執(zhí)行:

          php artisan migrate

          我們先忽視創(chuàng)建 contacts 表出現(xiàn)的錯誤,刷新 sequel pro 查看一下:

          匯總laravel migrate的一些常見錯誤

          果然,migration 表中沒有 contacts 的創(chuàng)建記錄,這也就難怪執(zhí)行 reset 的時候,會沒有 contacts 的回滾操作了。

          contacts 無法創(chuàng)建 branch_id 外鍵的解決

          現(xiàn)在,我們已經(jīng)執(zhí)行了 migrate 命令,我們重新來看看這個最早出現(xiàn)的錯誤:

          [IlluminateDatabaseQueryException]    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 中手工執(zhí)行一下這條 SQL 語句。

          匯總laravel migrate的一些常見錯誤

          果然,執(zhí)行返回錯誤。

          仔細查看語句并沒有錯誤,一想,應該是 branch_id 類型聲明和 branches 表中的 ID 類型不一致造成的吧。查看 contacts 的結(jié)構(gòu),發(fā)現(xiàn) Unsigned 沒有打鉤,勾選后再執(zhí)行增加外鍵的 SQL 語句,成功。

          匯總laravel migrate的一些常見錯誤

          找到問題原因后,我們就清空數(shù)據(jù)庫,修改 contacts 的 migration 文件,調(diào)整 branch_id 為:

          $table->integer('branch_id')->unsigned()->comment('機構(gòu)ID');

          再重新執(zhí)行 migrate 命令,成功!

          匯總laravel migrate的一些常見錯誤

          贊(0)
          分享到: 更多 (0)
          網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號