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.DateTimeField(auto_now=True) class Meta: unique_together = ('category', 'date')
category 表示考試的類型, date 表示考試的日期。建表的時候考慮到一個類型的考試在同一個應該只有一個考試,所以就加了一個 unique_together
。但是由于業(yè)務需要,這個 unique_together
不需要了。
用過 django 的人都知道,這不是個大問題,刪掉 unique_together
的代碼,然后 makemigrations
唄,確實,我就這么做了。但是當我 migrate
的時候卻報錯了,錯誤如下:
數(shù)據(jù)庫不讓我刪除這個 index ,并且告訴我有一個 外鍵約束 用到了這個它。我就奇怪了,category
是外鍵沒錯,但是我這個是 unique_together
啊,怎么可能有哪個外鍵用到了它呢?
沒辦法,我只能到數(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
外鍵。沒有其他東西了啊,到底哪個外鍵用到了我們的 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 在聯(lián)合索引的左邊,根據(jù) 最左前綴 原則, category 的索引就有了,所以就不會另外建索引,這個時候 category 的外鍵約束就依賴了這個 unique_key ,所以刪除的時候會出現(xiàn)那樣的報錯。
機智的小伙伴應該想到了,如果我們要去掉 unique_together
,我們可以將 category
的 KEY
加回去,這樣就可以將 unique_together
刪掉了。 sql 如下:
alter table exam add index(category_id);
這樣,migrate就能成功了。
- 解決django migrate報錯ORA-02000: missing ALWAYS keyword
- Django 解決阿里云部署同步數(shù)據(jù)庫報錯的問題
- Django-migrate報錯問題解決方案
- 基于Django實現(xiàn)日志記錄報錯信息
- 解決django model修改添加字段報錯的問題
- Django之創(chuàng)建引擎索引報錯及解決詳解
- Django重裝mysql后啟動報錯:No module named ‘MySQLdb’的解決方法
- django啟動uwsgi報錯的解決方法
- django項目運行因中文而亂碼報錯的幾種情況解決
- Django如何實現(xiàn)密碼錯誤報錯提醒
相關(guān)文章
python遍歷迭代器自動鏈式處理數(shù)據(jù)的實例代碼
迭代器也是用來遍歷對象成員的,下面這篇文章主要給大家介紹了關(guān)于python遍歷迭代器自動鏈式處理數(shù)據(jù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2022-01-01Python使用django框架實現(xiàn)多人在線匿名聊天的小程序
很多網(wǎng)站都提供了在線匿名聊天的小功能,下面小編基于python的django框架實現(xiàn)一個多人在線匿名聊天的小程序,具體實現(xiàn)代碼大家參考下本文2017-11-11Python利用filestools模塊實現(xiàn)水印添加
最近發(fā)現(xiàn)的這款filestools非標準庫其實真正實現(xiàn)添加水印的只要一個函數(shù)的調(diào)用,一行代碼即可完成水印的添加,感興趣的快跟隨小編一起學起來吧2022-09-09