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))
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); }
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!