【问题标题】:Php artisan migrate fails Laravel [duplicate]Php工匠迁移失败Laravel [重复]
【发布时间】:2017-12-12 14:09:56
【问题描述】:

我有以下错误。有人知道为什么吗?

php 工匠迁移

 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`))

create_users_table.php

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

【问题讨论】:

  • 这是因为 email 字段使用的字符集。 1 个字符不会是 1 个字节,而是 可能 4 个字节。当您尝试使该字段唯一时,它会失败,因为该列对于索引来说太长了。解决此问题的方法是创建另一个包含email散列 的列。您可以将该列设为email_hash binary(20),然后在其中保存电子邮件的原始sha1 哈希并使其唯一。这使您的索引的长度始终为 20 字节,并且适用于您的用例。

标签: php laravel


【解决方案1】:

编辑AppServiceProvider.php文件,你会在app/Providers/AppServiceProvider.php找到这个文件

use Illuminate\Support\Facades\Schema;

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

然后运行

composer update

在您的终端上。然后尝试迁移你的脚本,它会解决你的问题。

【讨论】:

    【解决方案2】:

    感谢所有留言

    用下一个代码解决:

     in config/database.php in mysql section
    
    
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
     and replace them with with
    
    'charset' => 'utf8',
    'collation' => 'utf8_unicode_ci',
    

    【讨论】:

    • 现在错误信息实际上很有意义。我忽略了这个设置。我手动创建了我的数据库,实际上我使用了排序规则“utf8_unicode_ci”。
    【解决方案3】:

    你需要做的是在App\Providers\AppServiceProvider 文件上编辑你的 AppServiceProvider.php 并在启动方法中设置一个默认的字符串长度:

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

    然后手动删除数据库然后composer dump-autoloadphp artisan migrate

    【讨论】:

      猜你喜欢
      • 2016-04-06
      • 2020-01-22
      • 2018-12-02
      • 1970-01-01
      • 2017-11-29
      • 2016-07-08
      • 1970-01-01
      • 2020-03-22
      • 2019-04-22
      相关资源
      最近更新 更多