Laravel 5.4運行migrate命令報錯1071 Specified key was too long解決方案如下:
D:\Laravel5.4>php artisan migrate
Migration table created successfully.
[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
Laravel 默認使用 utf8mb4 字符,包括支持在數(shù)據(jù)庫存儲「表情」。如果你正在運行的 MySQL release 版本低于5.7.7 或 MariaDB release 版本低于10.2.2 ,為了MySQL為它們創(chuàng)建索引,你可能需要手動配置遷移生成的默認字符串長度,你可以通過調用 AppServiceProvider 中的 Schema::defaultStringLength 方法來配置它:
use Illuminate\Support\Facades\Schema;
/**
* 引導任何應用程序服務。
*
* @return void
*/
public function boot()
{
Schema::defaultStringLength(191);
}
或者你可以為數(shù)據(jù)庫開啟 innodb_large_prefix 選項,有關如何正確開啟此選項的說明請查閱數(shù)據(jù)庫文檔。
修改文件:\app\Providers\AppServiceProvider.php
修改之后的源代碼:
重新運行php artisan migrate命令創(chuàng)建數(shù)據(jù)庫
D:\phpStudy\Laravel5.4>php artisan migrate
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_000000_create_users_table
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated: 2014_10_12_100000_create_password_resets_table
D:\Laravel5.4>php artisan migrate
Migration table created successfully.
[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
Laravel 默認使用 utf8mb4 字符,包括支持在數(shù)據(jù)庫存儲「表情」。如果你正在運行的 MySQL release 版本低于5.7.7 或 MariaDB release 版本低于10.2.2 ,為了MySQL為它們創(chuàng)建索引,你可能需要手動配置遷移生成的默認字符串長度,你可以通過調用 AppServiceProvider 中的 Schema::defaultStringLength 方法來配置它:
use Illuminate\Support\Facades\Schema;
/**
* 引導任何應用程序服務。
*
* @return void
*/
public function boot()
{
Schema::defaultStringLength(191);
}
或者你可以為數(shù)據(jù)庫開啟 innodb_large_prefix 選項,有關如何正確開啟此選項的說明請查閱數(shù)據(jù)庫文檔。
修改文件:\app\Providers\AppServiceProvider.php
修改之后的源代碼:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Schema::defaultStringLength(191);
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}
重新運行php artisan migrate命令創(chuàng)建數(shù)據(jù)庫
D:\phpStudy\Laravel5.4>php artisan migrate
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_000000_create_users_table
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated: 2014_10_12_100000_create_password_resets_table