Laravel 6 – 1071 Specified key was too long; max key length is 767 bytes
When we migration of the table after the installation of the latest laravel at that time we face this type of error.
Because it’s not 520 bytes, but rather, 2080 bytes, which far exceeds 767 bytes, so we are facing this type of error.
Syntax error or access violation, #1071 – Specified key was too long; max key length is 767 bytes, PHP artisan migrate command error, long max key length is 767 bytes, syntax error or access violation 1071, MySQL error
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 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`)) at E:\xampp\htdocs\lara_auth\vendor\laravel\framework\src\Illuminate\Database\Connection.php:665 661| // If an exception occurs when attempting to run a query, we'll format the error 662| // message to include the bindings with SQL, which will make this exception a 663| // lot more helpful to the developer instead of just the database's errors. 664| catch (Exception $e) { > 665| throw new QueryException( 666| $query, $this->prepareBindings($bindings), $e 667| ); 668| } 669| Exception trace: 1 PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes") E:\xampp\htdocs\lara_auth\vendor\laravel\framework\src\Illuminate\Database\Connection.php:459 2 PDOStatement::execute() E:\xampp\htdocs\lara_auth\vendor\laravel\framework\src\Illuminate\Database\Connection.php:459 Please use the argument -v to see more details. |
Now. let’s go we will solve the above error. first, open the AppServiceProvider.php file from the app/Providers/ directory. after then we will load the schema “use Illuminate\Support\Facades\Schema;” on the top header and set the “Schema::defaultStringLength(191);” into boot method.
You can see the below code, therefore, then you can better understand.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <?php namespace App\Providers; use Illuminate\Support\ServiceProvider; use Illuminate\Support\Facades\Schema; class AppServiceProvider extends ServiceProvider { /** * Register any application services. * * @return void */ public function register() { // } /** * Bootstrap any application services. * * @return void */ public function boot() { // Schema::defaultStringLength(191); } } ?> |
After the above changes, you can again migrate the table and then it will be successfully migrated.
Read Also
Laravel 6 Image Upload Tutorial Example
Laravel 6 Ajax Image Upload Example Tutorial
Laravel 6 Multiple Image Upload Example Tutorial