I can’t get Django (1.5) to create MySQL UNIQUE indexes on 3 columns, even though I’ve followed every suggestion I found on SO. Here’s how my model looks like:
class Loc(models.Model):
rand = models.IntegerField()
sectiune = models.ForeignKey(Sectiune)
numar = models.IntegerField()
pret = models.FloatField()
def __unicode__(self):
return str(self.sectiune.nume) + ': R' + str(self.rand) + ' L' + str(self.numar)
class Meta:
unique_together = (("rand","sectiune","numar"),)
I really don’t get what’s wrong. I’ve seen a bug report that unique_together doesn’t work on foreign keys, but I’ve also seen that has been fixed. Any help?
Turns out Django is not that smart after all… It doesn’t know how to
ALTERa table to create theUNIQUEconstraint. I just had to delete the tables, runsyncdbagain, and the constraints were there 🙂