当前位置: 主机百科 » 资源 » 技术 » 正文

Laravel:SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length

laravel 5.4 版本改变默认的数据库字符集为utf8mb4。如果你运行MySQL v5.7.7或者更高版本,则不需要做任何事情。

否则你在运行 migrations 命令时,可能会碰到下面这个错误:

1
  [IlluminateDatabaseQueryException]  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))  SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes  

1
  我们可以在 appProvidersAppServiceProvider.php 文件里中的 boot 方法里设置一个默认值来解决这个错误:

1
  Schema::defaultStringLength(200);

完整的AppServerProvider.php文件代码如下:

1
  <?php  namespace AppProviders;    use IlluminateSupportServiceProvider;  use IlluminateSupportFacadesSchema;    class AppServiceProvider extends ServiceProvider  {      public function boot()      {          Schema::defaultStringLength(200);      }        public function register()      {          //      }  }  

未经允许不得转载:主机百科 » Laravel:SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length

相关文章