Laravel Tips : Specified key was too long error

Published on June 28, 2021

Since Laravel 5.4, It made a change to the default database character set to uses utf8mb4 which includes support for storing emojis.

If you are using MariaDB or older versions of MySQL you may face this error when trying to run migrations:

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

[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes

In order to fix this problem, you have to do is edit your AppServiceProvider.php file and inside the boot method set a default string length:


  use Illuminate\Support\Facades\Schema;

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

Everything should work as normal, please share this posts If it's helpful.

About Me

I am a full-stack Laravel developer based in Phnom Penh, Cambodia.. I'm also the creator of Rean Code.

My passions are open-source, managing software , and creating quality and maintainable products.

Follow me on Twitter: @putheakhem.

© Khem Puthea