Opened 12 years ago

Closed 12 years ago

Last modified 12 years ago

#19420 closed Bug (fixed)

prefetch_related does not cache results when the model is inherited

Reported by: idanzalz@… Owned by: nobody
Component: Database layer (models, ORM) Version: 1.4
Severity: Normal Keywords: prefetch_related inheritence
Cc: antonbaklanov@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

given the following models:

class ParentBase(models.Model): pass

class Parent(models.Model): pass

class ParentWithBase(ParentBase): pass

class Child(models.Model):
    parent = models.ForeignKey('Parent')
    parent_with_base = models.ForeignKey('ParentWithBase')

When I try to select a child model and prefetch the parents, the parent with the base performs another query which I am trying to avoid.
So my test code is:

# Create the child and parent objects
Child.objects.create(parent = Parent.objects.create(), parent_with_base = ParentWithBase.objects.create())
# Select the child and prefetch the parents
child = Child.objects.prefetch_related('parent','parent_with_base').all()[0]
with self.assertNumQueries(0):
    child.parent # This works just fine - no queries are emitted
with self.assertNumQueries(0):
    child.parent_with_base # TEST FAILS HERE - since one query is emitted

Change History (4)

comment:1 by Anton Baklanov, 12 years ago

Cc: antonbaklanov@… added

comment:2 by Russell Keith-Magee, 12 years ago

Resolution: worksforme
Status: newclosed

I put your test models and test code into a test project, and it failed under 1.4, but passes under 1.5b2. It appears this bug has been fixed since the release of 1.4.

comment:3 by anonymous, 12 years ago

Good to hear it works in 1.5, but worksforme is not the right resolution, since it is broken on 1.4 as reported.

comment:4 by Luke Plant, 12 years ago

Resolution: worksformefixed
Note: See TracTickets for help on using tickets.
Back to Top