﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
16043	Specialization cache should be filled/shared with parent object cache (multitable inheritance)	Piotr Czachur	Paulo	"Here is some multi-table inheritance example:
{{{
class Owner(models.Model):
    nick = models.CharField(max_length=100)

class Car(models.Model):
    owner = models.ForeignKey(Owner)
    some_id = models.CharField(max_length=100)
    
class MiniMorris(Car):
    mini_type = models.CharField(max_length=1)

}}}

{{{

- model: auto.Owner
  pk: 1
  fields:
    nick: Homer

- model: auto.Car
  pk: 1
  fields:
    owner: 1
    some_id: 'minii A'
    
- model: auto.MiniMorris
  pk: 1
  fields:
    mini_type: 'e'
}}}

{{{
>>> c = Car.objects.select_related('owner').get(pk=1)
>>> # SELECT ""auto_car"".""id"", ""auto_car"".""owner_id"", ""auto_car"".""some_id"", ""auto_owner"".""id"", ""auto_owner"".""nick"" FROM ""auto_car"" INNER JOIN ""auto_owner"" ON (""auto_car"".""owner_id"" = ""auto_owner"".""id"") WHERE ""auto_car"".""id"" = 1

>>> c.owner
<Owner: Owner object>
>>>  # No SQL, cache hit.

>>> m = c.minimorris
>>> # SELECT ""auto_car"".""id"", ""auto_car"".""owner_id"", ""auto_car"".""some_id"", ""auto_minimorris"".""car_ptr_id"", ""auto_minimorris"".""mini_type"" FROM ""auto_minimorris"" INNER JOIN ""auto_car"" ON (""auto_minimorris"".""car_ptr_id"" = ""auto_car"".""id"") WHERE ""auto_minimorris"".""car_ptr_id"" = 1 
>>> # SQL is fine, we need to obtain this specialization

>>> m.owner
<Owner: Owner object>
>>>  # SELECT ""auto_owner"".""id"", ""auto_owner"".""nick"" FROM ""auto_owner"" WHERE ""auto_owner"".""id"" = 1 
>>>  # Redundant SQL. This could be obtained from cache.

}}}

SQL issued by ""m.owner"" is redundant as owner is already cached in parent object.
"	Cleanup/optimization	closed	Database layer (models, ORM)	dev	Normal	fixed			Accepted	0	0	0	0	0	0
