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. JavaScript数组方法大全(推荐)

    原网址:http://www.jb51.net/article/87930.htm 数组在笔试中经常会出现的面试题,javascript中的数组与其他语言中的数组有些不同,为了方便之后数组的方法学习, ...

  2. maven <scope>provided</scope>

    今天开发web的时候,需要用到servlet-api,于是在pom.xml中添加依赖 <dependency> <groupId>javax.servlet</group ...

  3. RealtimeRendering III

    [RealtimeRendering III] 1.砖块渲染实例. 1)brick & mortar diffuse texture. 2)brick & mortar gloss t ...

  4. destructuring assignment

    [destructuring assignment] The destructuring assignment syntax is a JavaScript expression that makes ...

  5. Oracle 入门

    一.安装Oracle 11g 服务端 服务端安装教程:https://jingyan.baidu.com/article/363872eccfb9266e4aa16f5d.html 二.安装客户端 客 ...

  6. 安卓自动生成代码插件-Android code Generator(转)

    编辑推荐:稀土掘金,这是一个针对技术开发者的一个应用,你可以在掘金上获取最新最优质的技术干货,不仅仅是Android知识.前端.后端以至于产品和设计都有涉猎,想成为全栈工程师的朋友不要错过! 介绍 A ...

  7. AdminLTE 动画时间

    app.js AdminLTE.css

  8. uWSGI和WSGI区别

    uWSGI是一个Web服务器,它实现了WSGI协议.uwsgi.http等协议.Nginx中HttpUwsgiModule的作用是与uWSGI服务器进行交换.WSGI是一种Web服务器网关接口.它是一 ...

  9. 将秒数转为HH:MM:SS格式的时间

    /**  * 将秒数转为HH:MM:SS格式的时间  * @param $seconds  * @return string  */ public static function GetHHMMSSB ...

  10. Python compile() 函数

    Python compile() 函数  Python 内置函数 描述 compile() 函数将一个字符串编译为字节代码. 语法 以下是 compile() 方法的语法: compile(sourc ...