2017-07-08 66 views
3

我有以下错误。有人正在理解为什么?Php artisan migrate failed Laravel

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(); 
}); 
+0

这是因为性格与'email'现场使用设定的。 1个字符不会是1个字节,它可能是* 4个字节。当您尝试使该字段唯一时,它会失败,因为该列索引太长。解决这个问题的方法是创建另一个包含'email'的** hash **的列。你可以创建该列'email_hash binary(20)',然后保存该邮件的原始'sha1'哈希值并使其唯一。这使得你的索引总是长度为20个字节,并且适用于你的用例。 –

回答

1

,你所要做的就是编辑AppServiceProvider.php上App\Providers\AppServiceProvider文件和引导方法内部设置一个默认的字符串长度:

use Illuminate\Support\Facades\Schema; 

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

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

1

编辑AppServiceProvider.php文件,你会发现这个文件中app/Providers/AppServiceProvider.php

use Illuminate\Support\Facades\Schema; 

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

那么你的终端上运行

composer update 

。然后尝试迁移您的脚本,它会解决您的问题。

0

感谢所有消息

与下面的代码解决:

in config/database.php in mysql section 


'charset' => 'utf8mb4', 
'collation' => 'utf8mb4_unicode_ci', 
and replace them with with 

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