django.db.utils.OperationalError: (1071, 'Specified key was too long; max key length is 767 bytes')
环境介绍
Django (2.1) Python 3.5.5
mysqlclient (1.4.2.post1)
Mysql 5.6.28
RHEL 7.3
在migrate时候报错
model代码如下
class EnvironmentVariable(models.Model):
"""
环境变量表
"""
application = models.ForeignKey(Application, verbose_name="所属应用", related_name="environment_variables",
on_delete=models.CASCADE)
key = models.TextField(verbose_name="键")
value = models.TextField(verbose_name="值", null=True, blank=True)
这个表在migrate时候出现BLOB/TEXT column 'id' used in key specification without a key length"
BLOB/TEXT不能被用作索引,所以报错
于是修改key字段
key = models.CharField(max_length=255, verbose_name="键")
然后makemigration 继续migrate没问题
当修改key的 max_length
key = models.CharField(max_length=1024, verbose_name="键")
出现报错:
django.db.utils.OperationalError: (1071, 'Specified key was too long; max key length is 767 bytes')
索引用的字段长度不能大于255
发现:
表在migrate时候会添加 application_id 和key两个字段的唯一联合索引
名:paas_environmentvariable_application_id_key_0838cfb7_uniq 拦位 application_id,key 索引类型 Unique 索引方法BTREE
解决:在数据库表设计里面去掉拦位key的索引
观察了数据库里面其它用户所创建的表都是两个字段做的唯一联合索引
思考:Django model里面有个db_index=True 的参数是够可以解决停止在migrate时候把key这个字段加入索引
Django所用(傻瓜式,支持多种数据库类型)数据库操作模块在migrate索引添加机制原理分析,最好能分析源代码
参考:
https://github.com/aschn/drf-tracking/issues/29#issuecomment-264030924
https://github.com/victorx98/drf-tracking/commit/efc2350b202d78cc2db9aed2763e381bb84ccae1
有人在model加了这个
class Meta:
unique_together = ("application", "key")
django.db.utils.OperationalError: (1071, 'Specified key was too long; max key length is 767 bytes')的更多相关文章
- django.db.utils.OperationalError: (1071, 'Specified key was too long; max key length is 767 bytes');
在使用utf8mb4字符集的情况下,如果列存在索引,那么varchar的最大长度是191 数据库版本: 在使用utf8字符集的情况下,如果列存在索引,那么varchar的最大长度是255. 在大字段上 ...
- 在执行migrate的时候出现问题(错误见末尾): django.db.utils.OperationalError: (1045, "Access denied for user ‘ODBC‘@‘localho st‘ (using password: YES)")
Python框架之Django的数据库 在执行migrate的时候出现问题(错误见末尾) django.db.utils.OperationalError: (1045, "Access d ...
- django.db.utils.OperationalError: 1050解决方案
manage.py migrate时进行同步数据库时出现问题;django.db.utils.OperationalError: (1050, "Table '表名' already exi ...
- django.db.utils.OperationalError: (1045, "Access denied for user 'ODBC'@'localhost' (using password)
错误描述: 从SQLLITE数据库换为MYSQL数据库,执行 python manage.py migrate 命令时,报错:django.db.utils.OperationalError: (10 ...
- django.db.utils.OperationalError: 1045错误
django.db.utils.OperationalError: (1045, "Access denied for user 'root'@'localhost' (using pass ...
- django.db.utils.OperationalError: no such table: auth_user
关于使用django 首次创建超级管理员时,出现 django.db.utils.OperationalError: no such table: auth_user 错误 1.首先使用命 ...
- Why getting this error “django.db.utils.OperationalError: (1050, ”Table 'someTable' already exists“)”
0down votefavorite I am getting error like django.db.utils.OperationalError: (1050, "Table 's ...
- django.db.utils.OperationalError: (1050, "Table '表名' already exists)解决方法
django.db.utils.OperationalError: (1050, "Table '表名' already exists)解决方法 找到解决方案,执行: python mana ...
- 解决:django.db.utils.OperationalError: unable to open database file
这是一个从GitHub上下载的,一个网站项目的源码.想要在自己的电脑上运行,期间过程相当曲折,不过至此终于是完成了. 1.安装过程: python2->virtualenv->django ...
随机推荐
- 让Ubuntu使用阿里云国内源,解决下载速度慢问题。
阿里云镜像官方地址 http://mirrors.aliyun.com/ 找到最新源地址列表: http://www.linuxdiyf.com/linux/23163.html 软件包管理中心(推荐 ...
- sql语句语句中的正则查找
举例: select tncl_id from tncl where tncl_id regexp'^0065'; 有一表,数据有10万多条,其中某列数据示例如下: 100000-200000-300 ...
- 在iOS项目中引入MVVM
本文翻译自:http://www.objc.io/issue-13/mvvm.html.为了方便读者并节约时间,有些不是和文章主题相关的就去掉了.如果读者要看原文的话可以通过前面的url直接访问.作者 ...
- What Are Tango Poses?Tango姿态是什么?
What Are Tango Poses?什么是Tango姿态? As your device moves through 3D space, it calculates where it is ( ...
- 企业搜索引擎开发之连接器connector(二十一)
从上文中的QueryTraverser对象的BatchResult runBatch(BatchSize batchSize)方法上溯到CancelableBatch类,该类实现了TimedCance ...
- Gluster 常用命令
Gluster 常用命令1 服务器节点# gluster peer status //查看所有节点信息,显示时不包括本节点 # gluster peer probe NODE-NAME //添加节点 ...
- JKS与P12证书互转
keytool -importkeystore -srckeystore cacerts -srcstoretype JKS -deststoretype PKCS12 -destkeystore k ...
- Ubuntu 16.04.2 LTS 安装 jdk1.6 和 tomcat6 (一)
java和tomcat环境配置已经有很多教程和文章,最近项目需要配置Ubuntu 16.04.2下的古老的java6和tomcat 6,遇到小坑,特记录和分享. 网上的教程不是太新,就是太老,还有一些 ...
- Solr 使用自定义 Query Parser(短语查询,精准查询)
原文出处:http://blog.chenlb.com/2010/08/solr-use-custom-query-parser.html 由于 Solr 默认的 Query Parser 生成的 Q ...
- solr特点五: MoreLikeThis(查找相似页面)
在 Google 上尝试一个查询,您会注意到每一个结果都包含一个 “相似页面” 链接,单击该链接,就会发布另一个搜索请求,查找出与起初结果类似的文档.Solr 使用MoreLikeThisCompon ...