SQLSTATE 42000: Syntax error or access Violation: 1071 Specified key was Too long; max key length is 767 bytes with PHP

Asked

Viewed 23,344 times

9

After I created the project with the command composer create-project --prefer-dist laravel/laravel laravel-scout I have been making the following changes to the archive .env

DB_DATABASE=course
DB_USERNAME=root
DB_PASSWORD=1234

Then I created the database with the same name as course

Then I made the command php Artisan migrate within the project, and generated this error;

PS C:\Users\wladimir\Desktop\php\laravel-scout> php artisan migrate
Migration table created successfully.


  [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


PS C:\Users\wladimir\Desktop\php\laravel-scout>
  1. Can someone explain to me what the Error means?
  2. Could someone explain to me how to correct the mistake?

Note: I am Walking Xampp, is an application that installs PHP and Mysql.

1 answer

38


I’ve been there, too, the best solution I could come up with was to change the Appserviceprovider, so I don’t need to keep adjusting the fields every time I create a new Migration. According to the official documentation:

Laravel uses the utf8mb4 character set by default, which includes support for storing "emojis" in the database. If you are running a Mysql version older than version 5.7.7 or Mariadb before version 10.2.2, you may need to configure manually the default string length generated by migrations to that Mysql creates indexes for them. You can configure this by calling the method Schema::defaultStringLength in the Appserviceprovider:

To resolve this follow the steps below:

  1. Edit the file app Providers Appserviceprovider.php
  2. Add the namespace use Illuminate\Support\Facades\Schema;
  3. Within the method boot add Schema::defaultStringLength(191);

End result of file:

use Illuminate\Support\Facades\Schema;

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

For more information see documentation: https://laravel.com/docs/5.5/migrations

Browser other questions tagged

You are not signed in. Login or sign up in order to post.