在执行 migration 时报错如下:
[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes (SQL: alter table users add unique users_email_unique(email))
[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes
受影响版本:MySQL ≤ 5.7.7, MariaDB ≤ 10.2.2。
原因及解决方法如下。
Laravel uses the utf8mb4 character set by default, which includes support for storing “emojis” in the database. If you are running a version of MySQL older than the 5.7.7 release or MariaDB older than the 10.2.2 release, you may need to manually configure the default string length generated by migrations in order for MySQL to create indexes for them. You may configure this by calling the Schema::defaultStringLength method within your AppServiceProvider:
public function boot()
{
\Illuminate\Support\Facades\Schema::defaultStringLength(191);
}
Alternatively, you may enable the innodb_large_prefix option for your database.