django model去掉unique_together报错的解决方案_python

事情是这样的,我有一个存储考试的表

class Exam(models.Model):
 category = cached_fields.ForeignKeyField(Category)
 name = models.CharField(max_length=128)
 date = models.DateField()
 created_at = models.DateTimeField(auto_now_add=True)
 updated_at = models.DateTimeField(auto_now=True)

 class Meta:
 unique_together = ('category', 'date')

category 表示考试的类型, date 表示考试的日期。建表的时候考虑到一个类型的考试在同一个应该只有一个考试,所以就加了一个 unique_together 。但是由于业务需要,这个 unique_together 不需要了。

用过 django 的人都知道,这不是个大问题,删掉 unique_together 的代码,然后 makemigrations 呗,确实,我就这么做了。但是当我 migrate 的时候却报错了,错误如下:

复制代码 代码如下:

django.db.utils.OperationalError: (1553, "Cannot drop index 'insurance_exam_category_id_a430e581_uniq': needed in a foreign key constraint")

数据库不让我删除这个 index ,并且告诉我有一个 外键约束 用到了这个它。我就奇怪了,category是外键没错,但是我这个是 unique_together 啊,怎么可能有哪个外键用到了它呢?

没办法,我只能到数据库里寻找答案, show create table exam ,输出如下:

| insurance_exam | CREATE TABLE `insurance_exam` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(128) NOT NULL,
 `date` date NOT NULL,
 `created_at` datetime(6) NOT NULL,
 `updated_at` datetime(6) NOT NULL,
 `category_id` int(11) NOT NULL,
 PRIMARY KEY (`id`),
 UNIQUE KEY `insurance_exam_category_id_a430e581_uniq` (`category_id`,`date`),
 CONSTRAINT `insurance_exam_category_id_a2238260_fk_insurance_category_id` FOREIGN KEY (`category_id`) REFERENCES `insurance_category` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1062 DEFAULT CHARSET=utf8mb4 |

可以看到 UNIQUE KEY 那一行就是 unique_together ,下面一行是 category 外键。没有其他东西了啊,到底哪个外键用到了我们的 unique_together

外键只能是 category 了,也没有别的外键啊。到底是怎么回事呢?

原因是这样的: 在Mysql中外键会自动在表上添加一个index ,也就说如果没有unique_together,我们的表应该是这样的:

| insurance_exam | CREATE TABLE `insurance_exam` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(128) NOT NULL,
 `date` date NOT NULL,
 `created_at` datetime(6) NOT NULL,
 `updated_at` datetime(6) NOT NULL,
 `category_id` int(11) NOT NULL,
 PRIMARY KEY (`id`),
 KEY `category_id` (`category_id`),
 CONSTRAINT `insurance_exam_category_id_a2238260_fk_insurance_category_id` FOREIGN KEY (`category_id`) REFERENCES `insurance_category` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1062 DEFAULT CHARSET=utf8mb4 |

但是因为有了 unique_together unique_key ,并且 category 在联合索引的左边,根据 最左前缀 原则, category 的索引就有了,所以就不会另外建索引,这个时候 category 的外键约束就依赖了这个 unique_key ,所以删除的时候会出现那样的报错。

机智的小伙伴应该想到了,如果我们要去掉 unique_together ,我们可以将 category KEY 加回去,这样就可以将 unique_together 删掉了。 sql 如下:

alter table exam add index(category_id);

这样,migrate就能成功了。

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索django
, model
, unique
, together
unique_together
django model unique、python django model、unique together、django indextogether、django unique,以便于您获取更多的相关知识。

时间: 2024-09-17 04:48:58

django model去掉unique_together报错的解决方案_python的相关文章

django model去掉unique_together报错

事情是这样的,我有一个存储考试的表 class Exam(models.Model):  category = cached_fields.ForeignKeyField(Category)  name = models.CharField(max_length=128)  date = models.DateField()  created_at = models.DateTimeField(auto_now_add=True)  updated_at = models.DateTimeFie

javascript-js代码中加上两个alert就好用,去掉就报错

问题描述 js代码中加上两个alert就好用,去掉就报错 var mvc = new suspicionmanagement.views.MainViewController(); var view = mvc.getView() var v_dataGrid = view.getDataGrid(); v_dataGrid.load(); alert(v_dataGrid); var datas = v_dataGrid.items.length;//得到总记录数 alert(datas);

ubuntu 64位android项目报错的解决方案,打开64位 Ubuntu 的32位支持功能

ubuntu的64位下的android环境,说实话,还真得费点精力了,解决一个问题,又出来一个新问题. 小编昨天刚好不容易将android的环境搭建好了,这不,刚建了个项目,直接就报错,下面是罗列出的几条: 1. libstdc++.so.6:cannot open shared object file:no such file or directory 2. Description Resource Path Location Type Error executing aapt: Cannot

解决Python中字符串和数字拼接报错的方法_python

前言 众所周知Python不像JS或者PHP这种弱类型语言里在字符串连接时会自动转换类型,如果直接将字符串和数字拼接会直接报错. 如以下的代码: # coding=utf8 str = '你的分数是:' num = 82 text = str+num+'分 | 琼台博客' print text 执行结果 直接报错:TypeError: cannot concatenate 'str' and 'int' objects 解决这个方法只有提前把num转换为字符串类型,可以使用bytes函数把int

Unity3D启动报错的解决方案

在Windows Server 2003 下安装好Unity3D,启动时报错--"Failed to initialize unity graphics.",截图如下:      在网上搜了一下,说是要启用D3D加速,于是dxdiag打开DX诊断工具,发现D3D加速不可用:      继续google,有说可能是显卡没有装好,于是,将显卡驱动升级到最新版本.但是,问题依然没有解决. 经过一番折腾,终于找到解决方案: (1)在桌面空白处点击右键,进入属性-设置-高级-疑难解答,开启完全的

docker-compose启动报错,解决方案

[root@cache1 www]# docker-composeTraceback (most recent call last): File "/usr/bin/docker-compose", line 5, in <module> from pkg_resources import load_entry_point File "/usr/lib/python2.7/site-packages/pkg_resources.py", line 301

vs2013 在win7下,使用c++创建项目各种报错问题解决方案

错误1:提示缺少mfc100ud.dll 错误2:win7 vs2013 mfc程序找不到sdkddkver.h 错误3:error LNK1158: 无法运行"rc.exe"   等等; 在网上搜了好久,也尝试了好多解决方案,最终找到解决方案,注意:环境是win7+vs2013 解决方案:项目--右键--属性--配置属性--常规--平台工具集--选择Visual Studio 2013 -WindowsXP(v120_xp)

Windows电脑网络应用程序报错的解决方案

netsh 是从Windows XP sp2开始提供的功能强大的网络配置命令行工具. 是一个能够通过命令行操作几乎所有网络相关设置的接口,比如设置IP,DNS,网卡,无线网络等.   winsock是Windows网络编程接口,winsock工作在应用层,它提供与底层传输协议无关的高层数据传输编程接口.reset是对Winsock的重置操作.当执行完winsock的命令重启计算机后,需要重新配置IP等网络配置信息.   netsh winsock reset 是把它恢复到默认状态.作用是重置 W

WCF,帮忙看一下为什么报错

问题描述 下面是一个包含WCF回调的部分代码:namespaceService{[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession,ConcurrencyMode=ConcurrencyMode.Reentrant)]publicclassService1:IService1{publicvoidDoSomething(){//获取调用客户端的实例通道ISomeCallbackContractcallback=O