laravel报错1071 Specified key was too long; max key length is 1000 bytes

Laravel 默认使用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改用4字节长度的utf8mb4字符编码的原因之一。不过要注意的是,只有MySql 5.5.3版本以后才开始支持utf8mb4字符编码(查看版本:selection version();)。如果MySql版本过低,需要进行版本更新。

解决问题

1.首先确保MySql版本在5.5.3以上

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

public function boot()
{
    //设置默认字符串长度
    Schema::defaultStringLength(191);
}

原文地址:https://www.cnblogs.com/jxl1996/p/10340062.html

时间: 2024-10-09 02:05:50

laravel报错1071 Specified key was too long; max key length is 1000 bytes的相关文章

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

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 --报错:Parse error: syntax error, unexpected '}', expecting ';'

报错如下: 原因:少了一个 ' ; ' laravel --报错:Parse error: syntax error, unexpected '}', expecting ';' 原文地址:https://www.cnblogs.com/daikiti/p/8970600.html

laravel --报错:Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW)

报错如下: 原因:( ) { } [ ] 不对称,或者用错了. laravel --报错:Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW) 原文地址:https://www.cnblogs.com/daikiti/p/8970576.html

spring boot 整合kafka 报错 Exception thrown when sending a message with key='null' and payload=JSON to topic proccess_trading_end: TimeoutException: Failed to update metadata after 60000 ms.

org.springframework.kafka.support.LoggingProducerListener- Exception thrown when sending a message with key='null' and payload='{"dataDts":["20180329","20180328","20180327","20180326","20180323"]

hive报错: Specified key was too long; max key length is 767 bytes

DataNucleus.Datastore (Log4JLogger.java:error(115)) - An exception was thrown while adding/validating class(es) : Specified key was too long; max key length is 767 bytescom.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Specified key was too

Hive创建表时报错,Specified key was too long; max key length is 767 bytes

报错信息如下: FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. MetaException(message:javax.jdo.JDODataStoreException: An exception was thrown while adding/validating class(es) : Specified key was too long; max key length

ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes

今天在MySQL 5.6版本的数据库中修改InnoDB表字段长度时遇到了"ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes"错误,第一次遇到这个错误,遂花了点学习.研究过.总结这个问题. 我们先来创建一个测试表,构造这样的错误. mysql> use MyDB; Reading table information for completion of table and colum

php artisan migrate时出现 SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes解决办法

在测试队列中运用artisan命令行 php artisan queue:table php artisan migrate时出现 SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes Change database.php which is in config folder where it says 'charset' =