#36840: makemigrations not detecting adding UniqueConstraint to models.py
---------------------------------+--------------------------------------
Reporter: ClashCityWomble | Owner: Vishy Algo
Type: Uncategorized | Status: assigned
Component: Migrations | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------
Comment (by Vishy Algo):
Replying to [ticket:36840 ClashCityWomble]:
> 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!
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:
{{{#!python
class CollectionSegment(models.Model):
collection = models.ForeignKey(Collection, on_delete=models.CASCADE)
athletesegment = models.ForeignKey(AthleteSegment,
on_delete=models.CASCADE)
class Meta:
constraints = [
models.UniqueConstraint(
fields=['collection', 'athletesegment'],
name='composite_unique_key'
)
]
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36840#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion visit
https://groups.google.com/d/msgid/django-updates/0107019b881330c3-8ebc367a-68de-467a-8c62-2b5820c846e8-000000%40eu-central-1.amazonses.com.