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,不需要对字符编码做切换。

解决问题

  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);
    }
  3.  

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

  1. 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 ...

  2. 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 ...

  3. 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 ...

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

    Laravel 默认使用utf8mb4字符编码,而不是的utf8编码.因此运行php artisan migrate会出现如下错误: [Illuminate\Database\QueryExcepti ...

  5. 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 ...

  6. 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的字段设置 ...

  7. 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 ...

  8. 导入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 ...

  9. 索引长度过长 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 ...

随机推荐

  1. 运用active和hover实现导航栏的页面切换

    .nav ul li a:hover{ background: #3E6EDD;}.nav ul li a:hover img{ display: block;}.nav ul li a.active ...

  2. 新手必看ES6基础

    ES6 基础 一.新的变量声明方式 let/const 与var不同,新的变量声明方式带来了一些不一样的特性,其中最重要的两个特性就是提供了块级作用域与不再具备变量提升. 通过2个简单的例子来说明这两 ...

  3. JMeter学习(二十一)关联(转载)

    转载自 http://www.cnblogs.com/yangxia-test 话说LoadRunner有的一些功能,比如:参数化.检查点.集合点.关联,Jmeter也都有这些功能,只是功能可能稍弱一 ...

  4. 笔记-Python中逗号的作用

    1.用,去掉额外的换行符

  5. vue生命周期简介

    vue生命周期简介 生命周期的钩子 LifeCycle hooks 上面已经能够清晰的看到vue2.0都包含了哪些生命周期的钩子函数~~ 那么 执行顺序以及什么时候执行,我们上代码来看~~~ 生命周期 ...

  6. openstack(Pike 版)集群部署(四)--- Nova 部署

    一.介绍 参照官网部署:https://docs.openstack.org/nova/pike/install/    继续上一博客进行部署:http://www.cnblogs.com/weiji ...

  7. CodeForces - 920C Swap Adjacent Elements

    传送门:点我 You have an array a consisting of n integers. Each integer from 1 to n appears exactly once i ...

  8. jQuery Grid高级指南

    上周以为已经把jqgrid 表格这部分已经搞得差不多了,没想到在实际用的时候,出现了不少问题,重新把这块知识整理一下. 问题一:设置表格的自动刷新 问题的原因: 使用表格自带的增删改查的功能,编辑完数 ...

  9. swift - 画图截取图片 - 保存相册

    1.图片截取 func test(addView:UIView) -> UIImage?{ UIGraphicsBeginImageContextWithOptions(CGSize(width ...

  10. [leetcode]127. Word Ladder单词接龙

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...