Specified key was too Long – Laravel 5.4 Error

Laravel 5.4 updated with a number of change including character set update in migrations. Laravel 5.4 using utf8mb4 character set having emojis support. However MySQL v5.7.7 supports emojis, but if you are running 5.x.x version you don’t need to play customize your migration. While working with older versions of MySQL or MariaDB you may face […]

Laravel 5.4 updated with a number of change including character set update in migrations. Laravel 5.4 using utf8mb4 character set having emojis support. However MySQL v5.7.7 supports emojis, but if you are running 5.x.x version you don’t need to play customize your migration.

While working with older versions of MySQL or MariaDB you may face “max key length is 767 bytes” error in 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

Laravel – Specified Key Was Too Long

Laravel Migrations guide can solve specified key was too long error. AppServiceProvider.php file must be customized, the boot method implementation use to provide the default length.

  1. Navigate to your application > App > Providers > AppServiceProvider.php.
  2. Paste the following code to replace your boot method.
use Illuminate\Support\Facades\Schema;

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

If the above method fails to solve your problem you need to load doctrine/dbal dependency in your composer.

Modifying Laravel Columns:

doctrine/dbal dependency must be added before modifying a column in Laravel. The library Doctrine DBAL helps you to controls the column state and create the SQL queries according to column adjustment.

Open your command prompt an write.

composer require doctrine/dbal

Leave a Reply

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