laravel migration指定的密钥太长;最大密钥长度为767字节

时间:2018-09-17 14:54:55

标签: laravel migration

我正在尝试迁移laravel迁移,但出现错误:

Migrating: 2014_10_12_100000_create_password_resets_table

   Illuminate\Database\QueryException  : SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `password_
resets` add index `password_resets_email_index`(`email`))

我的代码是:

if (!Schema::hasTable('password_resets')) {
            Schema::create('password_resets', function (Blueprint $table) {
                $table->string('email')->index();
                $table->string('token');
                $table->timestamp('created_at')->nullable();
            });
        }

2 个答案:

答案 0 :(得分:4)

您可以通过手动设置字符串长度

$table->string('name', 191); // You can put any number in exchange of 191

其他

将其放入APP->提供程序-> AppServiceProvider

use Illuminate\Support\Facades\Schema;

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

答案 1 :(得分:0)

您应该在AppServiceProvider的启动方法中将默认字符串长度设置为191

use Illuminate\Support\Facades\Schema;

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