Laravel5.8 下使用 MySQL5.6 时, migrate 报错: 索引太长 ?

laravel5.8 下使用 mysql5.6 时, migrate 报错: 索引太长 ?

使用 mysql5.7 没有问题,可以正常 migrate,
使用 mysql5.6 时,就出现这个问题,把字段长度缩小可以解决,
laravel5.8 下使用 mysql5.6 时, migrate 报错: 索引太长 ?
但是不知道原因

附:数据库字符集都是 utf8mb4_general_ci

《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
nfangxu
最佳答案

处理方案:在 App\Providers\AppServiceProvider 的 boot () 方法中添加 \Illuminate\Support\Facades\Schema::defaultStringLength(191); 即可

6年前 评论
讨论数量: 4

网上的解答大部分是:

由于 MySQL Innodb 引擎表索引字段长度的限制为 767 字节,因此对于多字节字符集的大字段(或者多字段组合索引),创建索引会出现上面的错误。
以 utf8mb4 字符集 字符串类型字段为例:utf8mb4 是 4 字节字符集,则默认支持的索引字段最大长度是: 767 字节 / 4 字节每字符 = 191 字符,因此在 varchar (255) 或 char (255) 类型字段上创建索引会失败。

所以 mysql5.7 以上没有这个问题,还是 mysql 有什么配置的关系呢?

6年前 评论

@beatles

mysql 5.7 文档:
If innodb_large_prefix is enabled (the default), the index key prefix limit is 3072 bytes for InnoDB tables that use DYNAMIC or COMPRESSED row format. If innodb_large_prefix is disabled, the index key prefix limit is 767 bytes for tables of any row format.

innodb_large_prefix is deprecated and will be removed in a future release. innodb_large_prefix was introduced in MySQL 5.5 to disable large index key prefixes for compatibility with earlier versions of InnoDB that do not support large index key prefixes.

file

所以,就是不同版本 mysql 的 innodb_large_prefix 的设置问题

6年前 评论

索引太长 ❌
数据库太老 ✅

6年前 评论
nfangxu

处理方案:在 App\Providers\AppServiceProvider 的 boot () 方法中添加 \Illuminate\Support\Facades\Schema::defaultStringLength(191); 即可

6年前 评论