Posted at

php artisan migrate でずっこけたとき

php artisan migrate しようとするとエラーとなる。

$ php artisan migrate

Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table

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 `users` add unique `users_email_unique`(`email`))

at /home/kusanagi/yoyaku/laravel/vendor/laravel/framework/src/Illuminate/Database/Connection.php:665
661| // If an exception occurs when attempting to run a query, we'll format the error
662| // message to include the bindings with SQL, which will make this exception a
663| // lot more helpful to the developer instead of just the database's errors.
664| catch (Exception $e) {
> 665| throw new QueryException(
666| $query, $this->prepareBindings($bindings), $e
667| );
668| }
669|

Exception trace:

1 PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes")
/home/kusanagi/yoyaku/laravel/vendor/laravel/framework/src/Illuminate/Database/Connection.php:459

2 PDOStatement::execute()
/home/kusanagi/yoyaku/laravel/vendor/laravel/framework/src/Illuminate/Database/Connection.php:459

Please use the argument -v to see more details.

対策として色々あるけど私は下記を行う

\laravel\app\Providers\AppServiceProvider.php

下記2行を追記する

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema; <----追記

class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}

/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Schema::defaultStringLength(191); <----追記
}
}

あと忘れずに、migratした時のDBテーブルを削除する。

そして再度

$ php artisan migrate