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

黄舟
Release: 2023-03-16 18:32:01
Original
1855 people have browsed it

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
Copy after login

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()
 {
  //
 }
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!