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

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

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

附: 数据库字符集都是 utf8mb4_general_ci

《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
nfangxu
最佳答案

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

4年前 评论
讨论数量: 4

网上的解答大部分是:

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

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

4年前 评论

@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 的设置问题

4年前 评论

索引太长 ❌
数据库太老 ✅

4年前 评论
nfangxu

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

4年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!