Model gets Meta from abstract ancestor class in MTI
|Reported by:||Kronuz||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||1.6|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
I'm getting this validation error:
CommandError: One or more models did not validate: places.restaurant: "index_together" refers to name. This is not in the same model as the index_together statement. places.restaurant: "index_together" refers to address. This is not in the same model as the index_together statement.
From the following models:
from django.db import models class AbstractPlace(models.Model): name = models.CharField(max_length=50) address = models.CharField(max_length=80) class Meta: abstract = True index_together = (('name', 'address'),) class Place(AbstractPlace): pass class Restaurant(Place): serves_hot_dogs = models.BooleanField() serves_pizza = models.BooleanField()
Seemingly, the concrete model Place ends up with the inherited Meta from the abstract parent. After the Place model class is created, during ModelBase.__new__), any Meta (declared or inherited) should not be there for concrete classes.
This way, models would never have a Meta (unless the modified and explicitly added Meta, if they're abstract). I added new_class.Meta = None just above new_class._prepare() in ModelBase and that properly fixes the issue.
Change History (4)
Changed 14 months ago by Kronuz
comment:1 Changed 14 months ago by anubhav9042
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
comment:2 Changed 14 months ago by anubhav9042
- Cc anubhav9042@… added
- Component changed from Uncategorized to Database layer (models, ORM)
- Summary changed from Multi-table inheritance gets Meta from abstract ancestor model class to Model gets Meta from abstract ancestor class in MTI
- Triage Stage changed from Unreviewed to Accepted
- Type changed from Uncategorized to Bug