2017年8月13日 星期日

Lararvel 5.4執行migrate ,發生Specified key was too long error

Larval 5.4的資料庫格式做了些許的改變,預設的資料庫格式建議為utf8mb4 ,這可以讓使用者儲存emojis表情符號。但這樣的變格只適合Laravel 5.4新版本以及5.5.7以上的MySQL資料庫。

假如你的資料庫版本較舊,若執行php artisan make:migrate時,則會出現1071Specified key was too long error錯誤訊息



[Illuminate\Database\QueryException]                                                                                              
  SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table
  `users` add unique `users_email_unique`(`email`))                                                                              

  [PDOException]                                                                                                
  SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes
 
解決方法:
在app\Providers\AppServiceProvider.php再boot()方法中加上預設值
Schema::defaultStringLength(191);



參考網頁: