Django model removes the unique_together error solution, djangomodelunique
This is the case. I have a table that stores the exam.
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 indicates the examination type, and date indicates the examination date. When creating a table, consider that one type of examination should have only one test in the same one, so an additionalunique_together. However, due to business needsunique_together No.
Anyone who has used django knows that this is not a big problem. Delete it.unique_togetherAnd thenmakemigrationsWell, I did. But when ImigrateThe error is reported as follows:
Copy codeThe Code is as follows: django. db. utils. OperationalError: (1553, "Cannot drop index 'Insurance _ exam_category_id_a1_e581_uniq': needed in a foreign key constraint ")
The database does not allow me to delete this index and tells me that it is used by a foreign key constraint. That's strange to me,categoryThe foreign key is correct, but I amunique_togetherAh, how can I use a foreign key?
No way, I can only search for the answer in the database,show create table examThe output is as follows:
| 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 |
We can see thatUNIQUE KEY That line isunique_together The following line iscategory Foreign key. Nothing else. Which foreign key is used?unique_together?
The foreign key can only becategory No foreign keys. What is going on?
The reason is: the Foreign keys in Mysql will automatically add an index to the table, that is, if there is no unique_together, our table should be like this:
| 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 |
But because of thisunique_together Ofunique_key And category is on the left of the Union Index. According to the leftmost prefix principle, the index of category will be available, so no additional index will be created, at this time, the foreign key constraint of category depends on this unique_key. Therefore, this error will be reported during deletion.
Witty friends should have thought of it. If we want to remove itunique_together , We cancategory OfKEY Add it back so that you canunique_together Deleted. The SQL statement is as follows:
alter table exam add index(category_id);
In this way, migrate will be successful.