摘要:本文給大家分享的是在使用django model去掉unique_together時(shí)報(bào)錯(cuò)的解決思路和具體步驟,提供給大家參考下,希望對大家學(xué)習(xí)使用django能夠有所幫助事情是這樣的,我有一個(gè)存儲(chǔ)考試的表class Exam(models.Model): category = cached_fields.ForeignKeyField(Category)
本文給大家分享的是在使用django model去掉unique_together時(shí)報(bào)錯(cuò)的解決思路和具體步驟,提供給大家參考下,希望對大家學(xué)習(xí)使用django能夠有所幫助
事情是這樣的,我有一個(gè)存儲(chǔ)考試的表
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 表示考試的日期。建表的時(shí)候考慮到一個(gè)類型的考試在同一個(gè)應(yīng)該只有一個(gè)考試,所以就加了一個(gè) unique_together 。但是由于業(yè)務(wù)需要,這個(gè) unique_together 不需要了。
用過 django 的人都知道,這不是個(gè)大問題,刪掉 unique_together 的代碼,然后 makemigrations 唄,確實(shí),我就這么做了。但是當(dāng)我 migrate 的時(shí)候卻報(bào)錯(cuò)了,錯(cuò)誤如下:
django.db.utils.OperationalError: (1553, "Cannot drop index 'insurance_exam_category_id_a430e581_uniq': needed in a foreign key constraint")
數(shù)據(jù)庫不讓我刪除這個(gè) index ,并且告訴我有一個(gè) 外鍵約束 用到了這個(gè)它。我就奇怪了,category是外鍵沒錯(cuò),但是我這個(gè)是 unique_together 啊,怎么可能有哪個(gè)外鍵用到了它呢?
沒辦法,我只能到數(shù)據(jù)庫里尋找答案, 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 外鍵。沒有其他東西了啊,到底哪個(gè)外鍵用到了我們的 unique_together ?
外鍵只能是 category 了,也沒有別的外鍵啊。到底是怎么回事呢?
原因是這樣的: 在Mysql中外鍵會(huì)自動(dòng)在表上添加一個(gè)index ,也就說如果沒有unique_together,我們的表應(yīng)該是這樣的:
| 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 |
但是因?yàn)橛辛?nbsp;unique_together 的 unique_key ,并且 category 在聯(lián)合索引的左邊,根據(jù) 最左前綴 原則, category 的索引就有了,所以就不會(huì)另外建索引,這個(gè)時(shí)候 category 的外鍵約束就依賴了這個(gè) unique_key ,所以刪除的時(shí)候會(huì)出現(xiàn)那樣的報(bào)錯(cuò)。
機(jī)智的小伙伴應(yīng)該想到了,如果我們要去掉 unique_together ,我們可以將 category 的 KEY 加回去,這樣就可以將 unique_together 刪掉了。 sql 如下:
alter table exam add index(category_id);
這樣,migrate就能成功了。
更多關(guān)于django model去掉unique_together報(bào)錯(cuò)的解決方案請關(guān)注PHP中文網(wǎng)(ipnx.cn)其他文章!