Laravel5.4에서 migration 실행시에 다음과 같은 에러가 발생하였다.
$ 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 767 bytes (SQL: alter table `u
sers` add unique `users_email_unique`(`email`))
[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes
이 문제에 대한 원인과 해결방법을 Laravel 공식문서에서 찾을 수 있었다.
https://laravel.com/docs/5.4/migrations#creating-indexes
Index Lengths & MySQL / MariaDB
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:
Laravel은 데이터베이스에 "emojis"를 저장하기 위해 utf8mb4 캐릭터셋을 사용한다고 한다. MySQL 버전이 5.7.7보다 낮거나 MariaDB 버전이 10.2.2보다 낮은 경우에, migration시에 생성된 default string length를 수동으로 재설정해야 인덱스가 제대로 생성된다고 한다. 해결방법은 AppServiceProvider 클래스의 boot() 함수에서 Schema::defaultStringLength(191) 함수를 호출해주면 된다.
• app/Providers/AppServiceProvider.php
use Illuminate\Support\Facades\Schema; --> 코드 추가
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Schema::defaultStringLength(191); --> 코드 추가
}