Lapin-Blanc
Lapin-Blanc

Reputation: 1985

makemigrations doesn't see changes after adding unique_together constraint

Using django version 1.11.2 final.

I have the model below, for small invoices. I made several migrations along the way, and eventually added the unique_together = ('facture', 'article') constraint into the 'Line' model. However, when running ./manage.py makemigrations, no changes are detected. Also tried with unique_together = (('facture', 'article'),) syntax, but get the same result.

from django.db import models
from django.db.models import F, Sum
from personnes.models import Person

# Create your models here.
class Facture(models.Model):
    date = models.DateField(auto_now=True)
    client = models.ForeignKey(Person, related_name="factures", on_delete= models.CASCADE)
    articles = models.ManyToManyField("Article", through="Line", related_name="factures")

    @property
    def total(self):
        return self.lines.aggregate(total=Sum(F('count')*F('article__price')))

    def __str__(self):
        return "%s %s %s" % (self.id, self.date, self.client.first_name)

class Article(models.Model):
    name = models.CharField(max_length=100)
    price = models.DecimalField(max_digits=10, decimal_places=2)

    def __str__(self):
        return "%s %s" % (self.name, self.price)

class Line(models.Model):
    facture = models.ForeignKey(Facture, related_name="lines", on_delete=models.CASCADE)
    article = models.ForeignKey(Article, related_name="lines", on_delete=models.CASCADE)
    count = models.IntegerField(default=1)

    @property
    def amount(self):
        return self.count * self.article.price

    def __str__(self):
        return "facture %s - %s x %s %s" % (self.facture.id, self.count, self.article.name, self.article.price)

    class META:
        unique_together = ('facture', 'article')

Upvotes: 2

Views: 392

Answers (1)

Alasdair
Alasdair

Reputation: 308899

You have capitalised your meta class incorrectly. It should be

class Line(models.Model):
    ...

    class Meta:
        unique_together = ('facture', 'article')

Upvotes: 2

Related Questions