Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes

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()
    {
        //
    }

你可能感兴趣的:(Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes)