Laravel 5.4 Solution of migrations error report due to too long special field

  • 2021-08-10 07:17:25
  • OfStack

Preface

This article mainly introduces the related contents of migrations error reporting due to the long special field of Laravel 5.4, and shares it for your reference and study. The following words are not said much, let's take a look at the detailed introduction:

laravel 5.4 changed the default database character set, and now utf8mb4 includes support for storing emojis. MySQL requires v 5.7. 7 or later. When you try to run migrations on 1 MariaDB or 1 older MySQL, you will 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 `use
 rs_email_unique`(`email`))
 
 [PDOException]
 SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes

Solution

After querying, we can set a default value in the boot method in the AppServiceProvider. php file:


<?php
namespace App\Providers;
 
use Illuminate\Support\ServiceProvider;
// Added code 
use Illuminate\Support\Facades\Schema;
 
class AppServiceProvider extends ServiceProvider
{
 /**
  * Bootstrap any application services.
  *
  * @return void
  */
 public function boot()
 {
  // Added code 
  Schema::defaultStringLength(191);
 }
 
 /**
  * Register any application services.
  *
  * @return void
  */
 public function register()
 {
  //
 }
}

Summarize


Related articles: