首页 > 解决方案 > 默认 create_users_table 抛出最大密钥长度错误

问题描述

我通过在命令提示符窗口中执行这些代码行来启动我的 Laravel 项目。

cd C:\xampp\htdocs
composer global require laravel/installer
laravel new iezon

然后我执行了构建身份验证方案的命令,我还没有编辑任何东西。

artisan make:auth

然后我转到我的配置文件夹,在里面database.php,我用覆盖它的文件mysql同样将其更改为正确的信息。.env

现在,我创建迁移表来检查我与数据库的连接,方法是:

php artisan migrate:install

并安装我的表(默认用户表):

php artisan migrate:fresh

然后在此调试中引发错误:

Dropped all tables successfully.
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table

   Illuminate\Database\QueryException  : 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`))

  at C:\xampp\htdocs\iezon\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664
    660|         // If an exception occurs when attempting to run a query, we'll format the error
    661|         // message to include the bindings with SQL, which will make this exception a
    662|         // lot more helpful to the developer instead of just the database's errors.
    663|         catch (Exception $e) {
  > 664|             throw new QueryException(
    665|                 $query, $this->prepareBindings($bindings), $e
    666|             );
    667|         }
    668|

  Exception trace:

  1   PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes")
      C:\xampp\htdocs\iezon\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458

  2   PDOStatement::execute()
      C:\xampp\htdocs\iezon\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458

任何想法我错过了什么命令或做错了什么?除了具有正确信息的database.php文件和文件之外,我没有更改任何文件内容。我怎样才能解决这个问题?.envmysql

标签: phplaravellaravel-5.2

解决方案


Go To AppServiceProvider.php in ```app\Providers```

boot()通过添加此行进行更改

\Schema::defaultStringLength(191);


function boot()
{
 \Schema::defaultStringLength(191);
}

Laravelutf8mb4默认使用字符集,其中包括支持在数据库中存储“表情符号”。如果您运行的 MySQL 版本早于该5.7.7版本或 MariaDB 版本早于该10.2.2版本,您可能需要手动配置迁移生成的默认字符串长度,以便 MySQL 为它们创建索引。您可以通过Schema::defaultStringLength在您的AppServiceProvider


推荐阅读