laravel错误1071 Specified key was too long; max key length is 1000 bytes
Laravel 5.5 环境,php artisan migrate 之后,出现错误如题。
检查了一下,代码是这样的:
$table->increments('id');
$table->string('username')->uniq();
$table->string('email')->nullable()->uniq();
$table->text('avatar_url')->nullable();
$table->string('phone')->nullable();
$table->string('password');
$table->text('intro')->nullable();
$table->timestamps();
最后,把 uniq() 取消后,功能正常了。
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,不需要对字符编码做切换。
解决问题
升级MySql版本到5.5.3以上。
手动配置迁移命令
migrate生成的默认字符串长度,在AppServiceProvider中调用Schema::defaultStringLength方法来实现配置:use Illuminate\Support\Facades\Schema; /**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Schema::defaultStringLength(191);
}
laravel错误1071 Specified key was too long; max key length is 1000 bytes的更多相关文章
- laravel 错误 1071 Specified key was too long; max key length is 1000 bytes
laravel 执行 php artisan migrate 安装数据库报 1071 Specified key was too long; max key length is 1000 bytes ...
- laravel migrate时报错:Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes
今天在学习laravel的路由模型绑定时,在按照文档执行php artisan migrate时报错. In Connection.php line 664: SQLSTATE[42000]: Syn ...
- OpenStack安装keyston 错误BError: (pymysql.err.InternalError) (1071, u‘Specified key was too long; max key length is 767 bytes‘) [SQL: u‘\nCREATE TABLE migrate_ver
折腾了两天的错误,BError: (pymysql.err.InternalError) (1071, u‘Specified key was too long; max key length is ...
- laravel报错1071 Specified key was too long; max key length is 1000 bytes
Laravel 默认使用utf8mb4字符编码,而不是的utf8编码.因此运行php artisan migrate会出现如下错误: [Illuminate\Database\QueryExcepti ...
- flask建表遇到的错误: flask,sqlalchemy.exc.OperationalError: (MySQLdb._exceptions.OperationalError) (1071, 'Specified key was too long; max key length is 767 bytes')
error:flask,sqlalchemy.exc.OperationalError: (MySQLdb._exceptions.OperationalError) (1071, 'Specifie ...
- MySQL错误“Specified key was too long; max key length is 1000 bytes”的解决办法
MySQL错误"Specified key was too long; max key length is 1000 bytes"的解决办法 经过查询才知道,是Mysql的字段设置 ...
- 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 le ...
- 导入sql文件报错:1071 Specified key was too long; max key length is 767 bytes
ref: https://stackoverflow.com/questions/1814532/1071-specified-key-was-too-long-max-key-length-is-7 ...
- 索引长度过长 ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes
1.发现问题 今天在修改innodb表的某个列的长度时,报如下错误: alter table test2 modify column id varchar(500); ERROR 1071 (4200 ...
随机推荐
- python添加到环境变量
1.命令 vi ~/.bashrc export PATH="/home/tiany/software/python/python3/3.6.1/bin:$PATH" export ...
- Android Studio 2.3.3上引入3.0上开发的项目遇到的问题
dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) debugImplementation project( ...
- 用Navicat for MySQL 连接 CentOS 6.5
navicat for mysql windows 端 连接mysql服务器 用SSH通道访问 , 新建连接时配置如下 (两张图解释) 1.配置SSH . 配置常规
- smtp扫描
nc扫描 nc -nv ip号 25 nmap扫描
- oracle中的trim()函数详解
1.先看一下Oracle TRIM函数的完整语法描述 TRIM([ { { LEADING | TRAILING | BOTH }[ trim_character ]| trim_character} ...
- new usercontrol
http://www.codeproject.com/Articles/12421/The-Grouper-A-Custom-Groupbox-Control
- Driver stacktrace: at org.apache.spark.scheduler.DAGScheduler.org$apache$spark$scheduler$DAGSchedul
在写Spark程序是遇到问题 Driver stacktrace: at org.apache.spark.scheduler.DAGScheduler.orgapacheapachesparksch ...
- 分布式处理框架MapReduce的深入简出
1).MapReduce的概述 2).MapReduce 编程模型 3).MapReduce架构 4).MapReduce编程 Google MapReduce论文,论文发表于2004年 Hadoop ...
- 【转】 mysql 数据优化
数据库优化离不开索引,如何理解索引? ---------------------------------------------------------------------------- 可以参考 ...
- 在.NET 4中用IIS部署WCF就这么简单
在.NET 3.5中,我们需要这样做: 1. 添加一个HelloService.svc文件,添加ServiceHost标记,在Service中添加WCF服务实现的名称,比如: <%@ Servi ...