select_related has issues with models linked by a FK which inherit from a common model
|Reported by:||loic84||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||master|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
Considering the following models:
class Fowl(models.Model): name = models.CharField(max_length=10) class Hen(Fowl): pass class Chick(Fowl): mother = models.ForeignKey(Hen)
Chick.objects.select_related().mother would return a Hen model instance, but the value of its fields inherited from Fowl would be set to the value of the fields of the queried Chick.
The issue lies somewhere in the query generation as I tried the generated query in an SQL shell and it indeed pulls the Chick fields inherited from Fowl twice. This has probably something to do with table aliases.
Unlike most other select_related issues that simply fail to provide an optimization, this issue returns a faulty output which is pretty serious. Also worth mentioning, the admin calls select_related on every field present in list_display which leads to ChangeList displaying completely incorrect data .
Change History (4)
Changed 4 years ago by loic84
comment:1 Changed 4 years ago by akaariai
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset