Django allows you to define custom UniqueConstraints
to specify which combinations of values are allowed in a row, but removing these later can be problematic when some ForeignKey
is involved, at least with MySQL it may throw a Cannot drop index '...': needed in a foreign key constraint
at you.
The example below shows you how to resolve such a situation in 3 small individual migrations:
- We start with an existing UniqueConstraint.
class MyModel(models.Model):
other_model = models.ForeignKey("OtherModel", on_delete=models.CASCADE)
name = models.CharField(max_length=128)