Code

Opened 20 months ago

Closed 19 months ago

Last modified 15 months 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

Attachments (0)

Change History (4)

comment:1 Changed 19 months ago by bak1an

  • Cc antonbaklanov@… added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 19 months ago by russellm

  • Resolution set to worksforme
  • Status changed from new to closed

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 Changed 15 months ago by anonymous

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 Changed 15 months ago by lukeplant

  • Resolution changed from worksforme to fixed

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.