makemigrations not detecting adding UniqueConstraint to models.py
I recently updated my Django project to use Postgres instead od SQLLite. I wanted to add some composite unique keys to a couple of tables but after adding the constraint to the model, makemigrations doesnt detect any changes. If i make any other changes to the model like adding a new field then it detects that fine, creates the migration and I can apply it no problem. I'm a bit stuck. Do I just create the constraint through running some Postgres SQL or should it be managed through the models? Any help gratefully received!
class CollectionSegment(models.Model):
constraints = [
models.UniqueConstraint(
fields=['collection', 'athletesegment'],
name='composite_unique_key')
]
collection = models.ForeignKey(Collection, on_delete=models.CASCADE)
athletesegment = models.ForeignKey(AthleteSegment, on_delete=models.CASCADE)
Replying to ClashCityWomble:
The reason makemigrations isn't detecting the change is that the constraints list is defined directly on the model class. In Django, constraints must be placed inside the inner class Meta to be recognized by the migration framework.
You can fix your code like this: