iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 16
0
自我挑戰組

來用Laravel做點什麼吧系列 第 16

D16 如果你的Migrate無法使用⋯⋯

  • 分享至 

  • twitterImage
  •  

上次說到我的資料庫好像有點小問題⋯⋯好,是大問題。

我大概暫時解決了,所以來寫個解決方案。

理論上,如果資料庫成功連線,我們就可以使用Migration,所以如果想測試有沒有連上,用phpMyAdmin登入看看或是下 php artisan migrate 指令,有反應就是連上了。

理論上啦⋯⋯不知道其他人有沒有碰到,不過我遇到Migrate相關指令都會報錯的狀況。

(來個部分報錯內容截圖)

Day9時有講到資料庫連線設定,好不容易搞定這件事,結果Migrate就出問題。

本來以為是沒有連到資料庫,但打開phpMyAdmin確定可以登入,也能顯示資料庫,所以排除連線問題後,那就是指令這邊有問題。

看到有討論是針對MySQL版本,但我試過改版本的做法並重新啟動docker,毫無效果而且連登入都不行了,顯然問題不是在這。

最後找到這篇:Database: Migrations

Laravel uses the utf8mb4 character set by default, which includes support for storing "emojis" in the database. If you are running a version of MySQL older than the 5.7.7 release or MariaDB older than the 10.2.2 release, you may need to manually configure the default string length generated by migrations in order for MySQL to create indexes for them.

由於Laravel預設資料庫使用的編碼是utf8mb4,如果版本低於 5.7.7 就需要手動修改默認的字符長度。

配置用的檔案是住在app/ProvidersAppServiceProvider.php

use Illuminate\Support\Facades\Schema;

/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot()
{
    Schema::defaultStringLength(191);
}

記得要use第一行,我就是那個忘記use然後想怎麼又報錯的蠢蛋。

修正後把docker關掉,相關的cache檔案刪掉、家目錄的.laradock刪掉,重開。噹噹!

可喜可賀!可喜可賀!


上一篇
D15 Laravel的Test
下一篇
D17 要來做點什麼吧
系列文
來用Laravel做點什麼吧30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言