[Django] #36840: makemigrations not detecting adding UniqueConstraint to models.py

7 views
Skip to first unread message

Django

unread,
Jan 3, 2026, 7:32:02 PMJan 3
to django-...@googlegroups.com
#36840: makemigrations not detecting adding UniqueConstraint to models.py
---------------------------------+-----------------------------------------
Reporter: ClashCityWomble | Type: Uncategorized
Status: new | Component: Migrations
Version: 5.1 | Severity: Normal
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+-----------------------------------------
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)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36840>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jan 4, 2026, 1:17:26 AMJan 4
to django-...@googlegroups.com
#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
---------------------------------+--------------------------------------
Changes (by Vishy Algo):

* owner: (none) => Vishy Algo
* status: new => assigned

--
Ticket URL: <https://code.djangoproject.com/ticket/36840#comment:1>

Django

unread,
Jan 4, 2026, 3:15:23 AMJan 4
to django-...@googlegroups.com
#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

unread,
Jan 4, 2026, 3:36:47 AMJan 4
to django-...@googlegroups.com
#36840: makemigrations not detecting adding UniqueConstraint to models.py
-------------------------------+--------------------------------------
Reporter: Andy Gritton | 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 Andy Gritton):

That worked perfectly, thank you so much!
--
Ticket URL: <https://code.djangoproject.com/ticket/36840#comment:3>

Django

unread,
Jan 4, 2026, 8:26:24 AMJan 4
to django-...@googlegroups.com
#36840: makemigrations not detecting adding UniqueConstraint to models.py
-----------------------------------+--------------------------------------
Reporter: Clash City Womble | Owner: Vishy Algo
Type: Uncategorized | Status: closed
Component: Migrations | Version: 5.1
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------
Changes (by Jacob Walls):

* resolution: => invalid
* status: assigned => closed

--
Ticket URL: <https://code.djangoproject.com/ticket/36840#comment:4>
Reply all
Reply to author
Forward
0 new messages