Search code examples
laravellaravel-5.4

Laravel 5.4: Specified key was too long error


I have a problem using laravel migrate command. It shows :

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))

But my mysql version is 5.7.14 and my charset is :

mysql> show variables like "%char%";
+--------------------------+-------------------------------------------------+
| Variable_name            | Value                                           |
+--------------------------+-------------------------------------------------+
| character_set_client     | utf8mb4                                         |
| character_set_connection | utf8mb4                                         |
| character_set_database   | utf8mb4                                         |
| character_set_filesystem | binary                                          |
| character_set_results    | utf8mb4                                         |
| character_set_server     | utf8mb4                                         |
| character_set_system     | utf8                                            |
| character_sets_dir       | D:\wamp64\bin\mysql\mysql5.7.14\share\charsets\ |
+--------------------------+-------------------------------------------------+

So there should not be any problem as doc said. Someone can help me?


Solution

  • The 1000 bytes mentioned in the error message points to your storage engine being MyISAM instead of InnoDB. The MySql changes related to the key length in 5.7.7 were specific to the InnoDB engine.

    You either need to move your table to the InnoDB storage engine, or if you have to use MyISAM, you'll need to use the Schema::defaultStringLength(191); statement mentioned in the documentation.

    // AppServiceProvider
    
    use Illuminate\Support\Facades\Schema;
    
    public function boot()
    {
        Schema::defaultStringLength(191);
    }