영화, 팝송, 뉴스를 보면서 무료로 영어공부, 영어듣기 연습

    

[Laravel5.4] migration시에 "Specified key was too long" 에러가 발생하는 문제의 해결방법

Posted on 2017-02-18 23:45:28


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); --> 코드 추가
}

 



Related Posts

[Laravel5] log rotation 설정 2017-02-26 21:45:15
[Laravel5] blade template에서 변수를 선언하는 방법 2017-02-17 20:32:52
[Laravel5] timestamps(updated_at column)를 touch하지 않고 update하는 방법 2017-02-09 16:31:18
[Laravel5] altisan command 사용법 요약 2017-02-02 21:38:55
[Laravel5] 실행된 sql query의 로그를 기록하는 방법 2017-01-26 20:42:10