Laravel的Eloquent:绕过191个字符的VARCHAR限制

Laravel's Eloquent: bypass the 191 characters VARCHAR limitation

this person, this person and this person所示,为了避免

SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes

您必须像这样修改 Laravel 项目的 AppServiceProvider.php

<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;              //add this

class AppServiceProvider extends ServiceProvider {
    public function boot() {
        Schema::defaultStringLength(191);           //and this
    }

    public function register() { }
}

但这种方法的缺点是我使用的VARCHARs被限制在191个字符以内,我想有一个比这更长的字段。我该怎么办?

感谢您的帮助。

You will have to upgrade your MySQL version to v5.7.7 or higher.

实际上,您可以在迁移文件中定义大小。示例

$table->string('field_name', 200);