时间:2021-07-01 10:21:17 帮助过:8人阅读
本文主要介绍了关于Laravel 5.4因特殊字段太长导致migrations报错的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍:
laravel 5.4 改变了默认的数据库字符集,现在utf8mb4包括存储emojis支持。MySQL 需要v5.7.7或者更高版本,当你试着在一些MariaDB或者一些老版本的的MySQL上运行 migrations 命令时,你会碰到下面这个错误:
[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
解决方法
经过查询,我们可以在 AppServiceProvider.php 文件里的 boot 方法里设置一个默认值:
<?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()
 {
  //
 }
}以上就是Laravel5.4框架中解决特殊字段太长导致migrations报错的方法的详细内容,更多请关注Gxl网其它相关文章!