Laravel 5.4 migrate報錯:Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `users` add unique `us ers_email_unique`(`email`))
public function up() { Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('email')->unique(); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); }
以上是user表的migartion,可以看出name字段並沒有聲明長度,laravel默認了1071,而報錯中看出數據庫設置了最大是767,所以就報錯了
Laravel 5.4默認使用utf8mb4
字符編碼,而不是之前的utf8
編碼。mb4的最大字符長度為4個字節,解決方法是:
手動配置遷移命令migrate生成的默認字符串長度,在AppServiceProvider中調用Schema::defaultStringLength方法來實現配置:
use Illuminate\Support\Facades\Schema; /** * Bootstrap any application services. * * @return void */ public function boot() { // 767/4 Schema::defaultStringLength(191); }