Home >Backend Development >PHP Tutorial >How to solve the problem of migration error caused by special fields being too long in Laravel5.4 framework

How to solve the problem of migration error caused by special fields being too long in Laravel5.4 framework

黄舟
黄舟Original
2017-10-24 09:07:001905browse

Preface

This article mainly introduces the relevant content about migration errors caused by Laravel 5.4 due to too long special fields. It is shared for everyone’s reference and study. I won’t say much below. 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 v5.7.7 or higher. When you try to run the migrations command on some MariaDB or some older versions of 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 query, we can set a default value in the boot method in the AppServiceProvider.php file:


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

The above is the detailed content of How to solve the problem of migration error caused by special fields being too long in Laravel5.4 framework. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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