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`))

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);
}

 

posted @ 2017-07-24 22:06  林豆包的长颈鹿  阅读(2869)  评论(0编辑  收藏  举报