migrate User表時報了這個錯
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes (SQL: alter table `users` add unique `users_emai
l_unique`(`email`))
原因:
Mysql數據庫字符串數據類型最大1000字節,laravel建表時字符串最大1071字節,主要是由于laravel用的是utf8mb4 字符集,每個字符占4個字節,Mysql用的是utf8字符集,每個字符占3個字節。
解決:
在AppServiceProvider
中的boot
方法中調用這個函數
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//修改字符長度為250,(字節數=250*4=1000)
Schema::defaultStringLength(250);
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}