首页 文章

Laravel Migration Error:语法错误或访问冲突:1071指定密钥太长;最大密钥长度为767字节laravel 5.3

提问于
浏览
4

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

怎么了?我正在使用laravel 5.3

5 回答

  • 0

    参考Laravel NewsLaravel's migration guide

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

    use Illuminate\Support\Facades\Schema;
    
    function boot()
    {
        Schema::defaultStringLength(191);
    }
    
  • 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%工作 .

  • 0

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

  • 5

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

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

    之后,一切都应该正常工作 .

  • 0

    如果,上面的解决方案或正在添加的官方解决方案

    Schema::defaultStringLength(191);
    

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

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

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

    它应该工作 . 希望能帮助到你 .

相关问题