Home >Database >Mysql Tutorial >Laravel 5.4 Migration Error: How to Fix 'Specified Key Was Too Long'?

Laravel 5.4 Migration Error: How to Fix 'Specified Key Was Too Long'?

Barbara Streisand
Barbara StreisandOriginal
2024-12-18 21:39:11819browse

Laravel 5.4 Migration Error: How to Fix

Laravel Migration Error: "Specified Key Was Too Long"

Problem Encountered:

When attempting to use the make:auth command in Laravel 5.4, a database migration error occurs:

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

Solution:

To resolve this issue, which is commonly encountered when using the default MySQL InnoDB engine, there are two approaches:

AppServiceProvider Method:

  1. Open app/Providers/AppServiceProvider.php.
  2. Add the following code inside the boot method:
use Illuminate\Support\Facades\Schema;

/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot()
{
    Schema::defaultStringLength(191);
}

MySQL Server Configuration:

Alternatively, you can modify the MySQL server configuration to enable the innodb_large_prefix option. The specific instructions for enabling this option vary depending on your database setup. Refer to MySQL documentation for guidance.

Additional Notes:

  • The solution provided is specific to Laravel 5.4. For other versions, refer to the relevant documentation.
  • The defaultStringLength(191) method sets the default maximum string length for all database columns to 191 characters.
  • The innodb_large_prefix option allows MySQL to use a larger prefix for indexes, resolving the key length limitation.

The above is the detailed content of Laravel 5.4 Migration Error: How to Fix 'Specified Key Was Too Long'?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn