Skip to content

Instantly share code, notes, and snippets.

@dheysonalves
Created January 25, 2021 16:03
Show Gist options
  • Save dheysonalves/07943bc83b7cb50f17eab75d4d43108c to your computer and use it in GitHub Desktop.
Save dheysonalves/07943bc83b7cb50f17eab75d4d43108c to your computer and use it in GitHub Desktop.
Laravel Error - Specified key was too long error

Laravel Error - Specified key was too long error

Problema

Quando rodando versões antigas do Maria ou do MySql - antes da versão 5.7.7 -, o erro é disparado no console do Laravel na tentativa de popular o banco com migration.

   [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 

Solução

Para solucionar, deve ser editado o arquivo de boot dos serviços do laravel, dentro da pasta provider, AppServiceProvider.php, na função boot().

  use Illuminate\Support\Facades\Schema;

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

Onde é determinado o tamanho padrão dos tipos string do banco de dados.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment