4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Laravel Specified key was too long; max key length is 767 bytes

Posted at

Laravelをローカル環境で開発した後に起こるアレ

keylengthが長すぎ問題。
厳密にはvarchar(255)が問題。

$table->string('name');

のような、デフォルトの設定がこうなっている。

原因

  1. Laravel5.4から標準charasetがutf8mb4に変わった

標準charasetがutf8mb4となったことで1文字あたりの最大byte数が4bytesに増えた。

  1. MySQLではユニーク制約を付けたカラムには767bytesまでしか入らない。

MySQLではPRIMARY_KEYおよびUNIQUE_KEYを付けたカラムには最大767bytesまでしか入らないらしい。ver5.7.7以降だと解消されている模様。

対策

app\Providers\AppServiceProvider.php

のboot()を書き換える。

php|AppServiceProvider.php
use Illuminate\Support\Facades\Schema;

public function boot()
{
    Schema::defaultStringLength(191);
}

これで起動できる。
マイグレーションが進んでしまっている可能性があるので、MySQL上で

drop database hogehoge;
create database hogehoge;

したほうが早い。

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?