laravel migration指定密钥太长;最大密钥长度为767字节[重复]

问题描述 投票:1回答:2

这个问题在这里已有答案:

我正在尝试迁移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();
            });
        }
laravel migration
2个回答
4
投票

你可以通过放置它来手动设置字符串长度

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

其他

把它放在APP - > Providers - > AppServiceProvider中

use Illuminate\Support\Facades\Schema;

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

0
投票

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

use Illuminate\Support\Facades\Schema;

public function boot() 
{
    Schema::defaultStringLength(191); 
}
© www.soinside.com 2019 - 2024. All rights reserved.