Opened 13 years ago
Closed 13 years ago
#19870 closed Bug (fixed)
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: | dev |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Accepted | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
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()[0].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 .
Attachments (1)
Change History (4)
by , 13 years ago
| Attachment: | select_related_regress.patch added |
|---|
comment:1 by , 13 years ago
comment:2 by , 13 years ago
| Triage Stage: | Unreviewed → Accepted |
|---|
comment:3 by , 13 years ago
| Resolution: | → fixed |
|---|---|
| Status: | new → closed |
This seems to be master-only regression. 1.4 and 1.5 do not have this according to the test. I know who git blame will point out for this issue...