【问题标题】:Illuminate\Database\QueryException SQLSTATE[42000]照亮\数据库\查询异常 SQLSTATE [42000]
【发布时间】:2019-03-08 11:55:30
【问题描述】:

我正在使用 Laravel,当我尝试运行 php artisan make:migrate 时,它显示此错误:

Illuminate\Database\QueryException : SQLSTATE[42000]: 语法错误或访问冲突:1071 指定的键太长;最大密钥长度为 767 字节(SQL:alter table users add unique users_email_unique(email))

在 /Users/soksan/Desktop/TestLaravel/multiUsers/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|

异常跟踪:

1 PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes")
/Users/soksan/Desktop/TestLaravel/multiUsers/vendor/laravel/framework/src/Illuminate/Database/Connection.php:458

2 PDOStatement::execute()
/Users/soksan/Desktop/TestLaravel/multiUsers/vendor/laravel/framework/src/Illuminate/Database/Connection.php:458

请使用参数-v 查看更多详情。

【问题讨论】:

标签: php mysql laravel


【解决方案1】:

进入App\Providers\AppServiceProvider 类并将引导方法更改为如下所示

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

【讨论】:

  • 非常感谢您的帮助!它对我有用。
  • 添加了命名空间:\Illuminate\Support\Facades\Schema::defaultStringLength(191);
  • 您可以在 PHP 文件顶部添加`use Illuminate\Support\Facades\Schema;`,而不是指定Schama 类的完整限定名
【解决方案2】:

转到您的 laravel 项目并在该项目中打开 App\Providers\AppServiceProvider 类并更改引导方法并在图像中添加新的模式显示。

在顶部添加这一行

use Illuminate\Support\Facades\Schema;

替换此方法

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

【讨论】:

    【解决方案3】:

    找到文件 config/database.php 并将字符集和排序规则从 utf8mb4 更改为 utf8

    'charset' => 'utf8', 'collat​​ion' => 'utf8_unicode_ci',

    【讨论】:

      猜你喜欢
      • 2019-04-25
      • 2023-03-09
      • 2019-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-20
      • 1970-01-01
      • 2018-07-14
      相关资源
      最近更新 更多