Laravel: Specified key was too long error

For older version of Laravel 5 and MySQL/MaraDb , you will encounter this error when running a migration code.

[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_uniq(email))

Laravel 5.4 started to use utf8mb4 as default database character set, which includes support for storing emojis. As long as you stay on using newer version of MySQL (v5.7.7 and higher) or MariaDB (10.2.2 and higher), you won’t encounter this error.

According to migration note from official Laravel page, you need to set the defaultStringLength in AppServiceProvider.php as below.

use Illuminate\Support\Facades\Schema;

public function boot()

For older Laravel, try setting a smaller length for the error column.

$table->string('email', 250);

If you application is new, set default charset to utf8mb4 in config/database.php file.

'mysql' => [
    'driver' => 'mysql',
    'host' => env('DB_HOST', ''),
    'port' => env('DB_PORT', '3306'),
    'database' => env('DB_DATABASE', 'laravel'),
    'username' => env('DB_USERNAME', 'root'),
    'password' => env('DB_PASSWORD', ''),
    'unix_socket' => env('DB_SOCKET', ''),
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'prefix' => '',
    'strict' => true,
    'engine' => null,

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.