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

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

Barbara Streisand
Release: 2024-12-16 04:16:13
Original
876 people have browsed it

Laravel Migration Error: How to Fix the

Laravel Migration Error: Addressing "Specified Key Was Too Long" Issue

When attempting to use Laravel 5.4's php artisan make:auth command, users may encounter the following error:

[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))
Copy after login

Cause:

The error occurs when the length of the index key exceeds the maximum allowed length for the database engine. By default, MySQL and its variants have a maximum index key length of 767 bytes.

Solution:

Method 1: Update Application Service Provider

As recommended in the official Laravel documentation, add the following code to your app/Providers/AppServiceProvider.php file:

use Illuminate\Support\Facades\Schema;

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

This increases the default string length for all migrations to 191 characters, which is within the allowed limit.

Method 2: Enable InnoDB Large Prefix

Alternatively, users can enable the innodb_large_prefix option for their MySQL database. Refer to the database's documentation for instructions on how to properly enable this option. This solution allows for longer index keys, making it more suitable for larger datasets.

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

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template