Opened 3 weeks ago

Closed 3 weeks ago

Last modified 3 weeks ago

#36840 closed Uncategorized (invalid)

makemigrations not detecting adding UniqueConstraint to models.py

Reported by: Clash City Womble Owned by: Vishy Algo
Component: Migrations Version: 5.1
Severity: Normal Keywords:
Cc: Clash City Womble Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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)

Change History (4)

comment:1 by Vishy Algo, 3 weeks ago

Owner: set to Vishy Algo
Status: newassigned

in reply to:  description comment:2 by Vishy Algo, 3 weeks ago

Replying to 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:

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'
            )
        ]

comment:3 by Clash City Womble, 3 weeks ago

That worked perfectly, thank you so much!

comment:4 by Jacob Walls, 3 weeks ago

Resolution: invalid
Status: assignedclosed

In the future, please see TicketClosingReasons/UseSupportChannels for places to get help.

Last edited 3 weeks ago by Tim Graham (previous) (diff)
Note: See TracTickets for help on using tickets.
Back to Top