select_related doesn't join parent chain for ForeignKeys on inherited models
|Reported by:||Owned by:||Jacob|
|Component:||Database layer (models, ORM)||Version:||master|
|Severity:||Keywords:||qsrf-cleanup select_related inheritance|
|Cc:||Malcolm Tredinnick||Triage Stage:||Accepted|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
Given this model where Pizza refers to ItalianRestaurant which is inherited from Restaurant:
class Restaurant(models.Model): # ... class ItalianRestaurant(Restaurant): # ... class Pizza(models.Model): restaurant = models.ForeignKey(ItalianRestaurant)
Queries to Pizza with select_related don't join Restaurant table and try to attach its fields to ItalianRestaurant table instead. Which results in wrong SQL.
Patch with tests follows. Though I believe that the patch is correct, logic of fill_related_selections seems to play on coincidental similarities between parent links and ordinary links and it may be better to refactor those into separate code paths. But I didn't penetrate it deep enough to do it.
Change History (11)
comment:3 Changed 8 years ago by
|Owner:||changed from nobody to Jacob|
|Status:||new → assigned|
|Triage Stage:||Unreviewed → Accepted|