Mitar
Mitar

Reputation: 7050

How to have abstract model with unique_together?

Why I am getting:

Error: One or more models did not validate:
test.test: "unique_together" refers to slug. This is not
in the same model as the unique_together statement.
test.test: "unique_together" refers to version. This is not
in the same model as the unique_together statement.

I have such model definition:

class SlugVersion(models.Model):
    class Meta:
        abstract = True
        unique_together = (('slug', 'version'),)

    slug = models.CharField(db_index=True, max_length=10, editable=False)
    version = models.IntegerField(db_index=True, editable=False)

class Base(SlugVersion):
    name = models.CharField(max_length=10)

class Test(Base):
    test = models.IntegerField()

I have Django 1.3.

Upvotes: 3

Views: 2028

Answers (2)

Mitar
Mitar

Reputation: 7050

It seems it is a bug in Django.

Upvotes: 2

sandinmyjoints
sandinmyjoints

Reputation: 1976

It's because Test is inheriting from Base using multi-table inheritance, which creates separate tables for the two models and links them with a OneToOne relationship. So the slug and version fields exist only in the Base table but not in the Test table.

Do you need Base to be non-abstract? In other words, will you create any actual Base objects? If you set it to be abstract, this error goes away.

Upvotes: 0

Related Questions