laravel 5.4 指定字符集太长的错误

最近在学习laravel框架,可是laravel一上来就给我个下马威,都是默认的配置为什么会出错呢,百度依旧不管用,github上面找了一下,欣慰的看到不是我一个人掉进坑中,这里就发出来解决方案希望不要有更多的人踩坑。

在laravel 5.4,改变了默认的数据库字符集,现在使用utf8mb4用于支持emojis
如果你使用MariaDB或低于MySQL v5.7.7时,执行php artisan migrate时会提示如下错误:

  [Illuminate\Database\QueryException]
  SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes (SQL: alter t
  able `users` add unique `users_email_unique`(`email`))

  [PDOException]
  SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes

当然了,要解决这个问题也非常简单

方案1:

修改app\Providers\AppServiceProvider.php

use Illuminate\Support\Facades\Schema;
//注意,默认是没有引用Schema的,需要手动引用

public function boot()
{
    Schema::defaultStringLength(191);
    //指定全局默认字符集长度
}

方案2:

修改默认的database\migrations\2014_10_12_000000_create_users_table.php(默认的users表)

Schema::defaultStringLength(191);
//添加默认的字符集长度
Schema::create('users', function (Blueprint $table) {
     $table->increments('id');
     $table->string('name');
     $table->string('email')->unique();
     $table->string('password');
     $table->rememberToken();
     $table->timestamps();
});

方案3:

修改默认的database\migrations\2014_10_12_000000_create_users_table.php(默认的users表)

Schema::create('users', function (Blueprint $table) {
     $table->increments('id');
     $table->string('name');
     $table->string('email', 250)->unique();
     //指定字符集长度
     $table->string('password');
     $table->rememberToken();
     $table->timestamps();
});

本文链接:

https://yubanmei.com/archives/18.html
1 + 2 =
快来做第一个评论的人吧~