【问题标题】:Laravel Migration Error : Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes laravel 5.3Laravel 迁移错误:语法错误或访问冲突:1071 指定的键太长;最大密钥长度为 767 字节 laravel 5.3
【发布时间】:2017-09-09 02:30:00
【问题描述】:

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

[PDOException] SQLSTATE[42000]:语法错误或访问冲突:1071 指定的密钥太长;最大密钥长度为 767 字节

怎么了?我正在使用 laravel 5.3

【问题讨论】:

标签: laravel laravel-5.3


【解决方案1】:

参考Laravel NewsLaravel's migration guide

正如迁移指南中所述,要解决此问题,您只需编辑 AppServiceProvider.php 文件并在引导方法中设置默认字符串长度:

use Illuminate\Support\Facades\Schema;

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

【讨论】:

  • 不工作...这项工作在 laravel 5.4 我尝试在 laravel 5.3 我得到同样的错误
  • 这在使用默认 config\database.php 设置 'engine' => null 时对我有用
【解决方案2】:
open create_user_tabel and then write one line inside up function.
  Schema::defaultStringLength(191);
e.g

    public function up()
    {      
      Schema::defaultStringLength(191);

    Schema::create('users', function (Blueprint $table) {
                $table->increments('id');
                $table->string('name');
                $table->string('email')->unique();
                $table->string('password');
                $table->rememberToken();
                $table->timestamps();
    }

它将 100% 工作。

【讨论】:

    【解决方案3】:

    该错误基本上来自数据库..如果您尝试通过 phpmyadmin 增加该列的长度,您将得到相同的错误。

    【讨论】:

      【解决方案4】:

      如迁移指南中所述,要解决此问题,您只需 编辑您的 AppServiceProvider.php 文件并在启动方法中设置一个 默认字符串长度:

      use Illuminate\Support\Facades\Schema;
      
      public function boot(){
          Schema::defaultStringLength(191);
      }
      

      之后一切都应该正常工作了。

      【讨论】:

        【解决方案5】:

        如果是上述解决方案或官方正在添加的解决方案

        Schema::defaultStringLength(191);
        

        不起作用。尝试编辑 config 文件夹中的 database.php 文件。只需编辑

        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        

        'charset' => 'utf8',
        'collation' => 'utf8_unicode_ci',
        

        它应该可以工作。希望对您有所帮助。

        【讨论】:

          【解决方案6】:

          更新您的/app/Providers/AppServiceProvider.php 以包含:

          use Illuminate\Support\Facades\Schema;
          
          /**
           * Bootstrap any application services.
           *
           * @return void
           */
          public function boot() {
              Schema::defaultStringLength(191);
          }
          

          【讨论】:

            【解决方案7】:

            如果其他建议都不起作用。这对我有用:

            config/database.php 中替换:

            'engine' => null',
            

            与:

            'engine' => 'InnoDB ROW_FORMAT=DYNAMIC',
            

            来源:https://laracasts.com/discuss/channels/general-discussion/syntax-error-or-access-violation-1071-specified-key-was-too-long

            【讨论】:

              猜你喜欢
              • 2017-07-03
              • 2019-10-11
              • 2018-09-15
              • 2021-02-08
              • 2017-04-01
              • 2018-09-28
              • 2021-02-28
              • 1970-01-01
              • 2015-06-22
              相关资源
              最近更新 更多