Laravel 5.4 migrate时报错: Specified key was too long error

Laravel 5.4默认使用utf8mb4字符编码,而不是之前的utf8编码。因此运行php artisan migrate 会出现如下错误:

[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 users_email_unique(email))

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

问题根源

MySql支持的utf8编码最大字符长度为3字节,如果遇到4字节的宽字符就会出现插入异常。三个字节UTF-8最大能编码的Unicode字符是0xffff,即Unicode中的基本多文种平面(BMP)。因而包括Emoji表情(Emoji是一种特殊的Unicode编码)在内的非基本多文种平面的Unicode字符都无法使用MySql的utf8字符集存储。

这也应该就是Laravel 5.4改用4字节长度的utf8mb4字符编码的原因之一。不过要注意的是,只有MySql 5.5.3版本以后才开始支持utf8mb4字符编码(查看版本:selection version();)。如果MySql版本过低,需要进行版本更新。

注:如果是从Laravel 5.3升级到Laravel 5.4,不需要对字符编码做切换。

解决问题

1.升级MySql版本到5.5.3以上。

2.手动配置迁移命令migrate生成的默认字符串长度,在AppServiceProvider中调用Schema::defaultStringLength方法来实现配置

    use Illuminate\Support\Facades\Schema;

/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
   Schema::defaultStringLength(191);//有试过貌似一定要用191?255还是报错?
}

3.关于不能改255 的原因:某个表设置了varchar为主键

MySQL的varchar主键只支持不超过768个字节 或者 767/2 ≈ 383个双字节 如GBK,或者 767/3 ≈ 255个三字节的字段 (UTF-8)...

utf8mb4 是四字节 所有767/4 ≈ 191....

原文地址:https://segmentfault.com/a/1190000008416200

时间: 2024-08-27 05:11:29

Laravel 5.4 migrate时报错: Specified key was too long error的相关文章

Laravel 5 migrate时报错: Specified key was too long error

Laravel 5.4默认使用utf8mb4字符编码,而不是之前的utf8编码.因此运行php artisan migrate 会出现如下错误: [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

laravel migrate时报错:Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes

在按照文档执行php artisan migrate时报错. 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 `users_email_unique`(`email`)) 解决方法 namespace App\Providers; use Illum

Laravel 5.4 migrate报错:Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `users` add unique `us ers_email_unique`(`email`))

Laravel 5.4 migrate报错:Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table `users` add unique `us     ers_email_unique`(`email`)) public function up() { Schema::create('users', function (Blu

EF Code First更新数据库时报错:provider: SQL Network Interfaces, error: 26

在使用EF Code First更新数据库时报如下错误: 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误.未找到或无法访问服务器.请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接. (provider: SQL Network Interfaces, error: 26 - 定位指定的服务器/实例时出错) 以为是Sql Server的配置问题,作如下尝试: →打开Sql Server配置管理器,即"Sql Server Configuraiotn

解决git clone时报错:The requested URL returned error: 401 Unauthorized while accessing

版本问题,最直接的解决办法就是重新编辑安装git吧: 1. 下载:# wget -O git.zip https://github.com/git/git/archive/master.zip 2. 解压:# unzip git.zip 3. 进入git目录:# cd git-master 4. 编译安装: autoconf ./configure --prefix=/usr/local make && make install 5. 最后别忘了删掉旧的git,并把新版本的git建立软链接

django配置使用mysql数据库运行报错:django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named 'MySQLdb'

今天在把django的默认数据库sqlite3切换为MySQL数据库时报错:django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named 'MySQLdb' 报错原因:django虚拟环境没有安装pymysql模块 解决: 先安装pymysql:pip install pymysql 然后在项目的 init.py 文件中添加以下代码: 把django的默认数据库sqlite3切

三星笔记本安装系统时报错:image failed to verify with * access denied* press any key to continue.

安装系统从光盘启动报错: 出现黑屏,并且有一个提示框image failed to verify with *access denied*press any key to continue 原因:三星笔记本对系统有降级保护,在将win8改装为win7是,需要设置bios. 1.开机按F2 进入bios,找到Boot-->secure boot-->改为:disabled---->OS Mode Selection改为:CSM OS. 2.在 advanced-->fast bios

在tomcat7下停止应用时报错:created a ThreadLocal with key of type

在tomcat7下停止应用时报错: created a ThreadLocal with key of type [com.opensymphony.xwork2.inject.ContainerImpl$10] INFO: A valid shutdown command was received via the shutdown port. Stopping the Server instance. Apr 02, 2015 10:36:52 AM org.apache.coyote.Abs

Mysql使用Navicat建立外键时报错cannot add foreign key constraint分析

Mysql使用Navicat建立外键时报错cannot add foreign key constraint分析 1)要关联的字段类型或长度不一致. 2)两个要关联的表编码不一样. 3)某个表已经有记录了. 4)将"删除时"和"更新时"都设置相同,如都设置成CASCADE. 原文地址:https://www.cnblogs.com/neymargoal/p/10072347.html