From d3b71b976dee4b02908235b8007db254a9795268 Mon Sep 17 00:00:00 2001 From: valtron Date: Fri, 11 Apr 2014 19:27:34 -0600 Subject: [PATCH] Fixed #21760 -- prefetch_related used an inefficient query for reverse FK. Regression introduced by commit 9777442. Refs #21410. --- AUTHORS | 1 + django/db/models/fields/related.py | 4 +++- docs/releases/1.6.3.txt | 4 ++++ tests/prefetch_related/tests.py | 18 ++++++++++++++++++ 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index 0bba6ce894..3adade2b19 100644 --- a/AUTHORS +++ b/AUTHORS @@ -635,6 +635,7 @@ answer newbie questions, and generally made Django that much better: Justine Tunney Maxime Turcotte Amit Upadhyay + valtron Adam Vandenberg Geert Vanderkelen Vasil Vangelovski diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index a789772dce..092898aedb 100644 --- a/django/db/models/fields/related.py +++ b/django/db/models/fields/related.py @@ -521,7 +521,9 @@ class ReverseSingleRelatedObjectDescriptor(object): # composite fields. In the meantime we take this practical approach to # solve a regression on 1.6 when the reverse manager in hidden # (related_name ends with a '+'). Refs #21410. - if self.field.rel.is_hidden(): + # The check for len(...) == 1 is a special case that allows the query + # to be join-less and smaller. Refs #21760. + if self.field.rel.is_hidden() or len(self.field.foreign_related_fields) == 1: query = {'%s__in' % related_field.name: set(instance_attr(inst)[0] for inst in instances)} else: query = {'%s__in' % self.field.related_query_name(): instances} diff --git a/docs/releases/1.6.3.txt b/docs/releases/1.6.3.txt index 538c67e5dc..c6adbcf09c 100644 --- a/docs/releases/1.6.3.txt +++ b/docs/releases/1.6.3.txt @@ -67,5 +67,9 @@ Other bugfixes and changes * Fixed :djadmin:`changepassword` on Windows (`#22364 `_). +* Fixed regression in ``prefetch_related`` that caused the related objects + query to include an unnecessary join + (`#21760 `_). + Additionally, Django's vendored version of six, :mod:`django.utils.six` has been upgraded to the latest release (1.6.1). diff --git a/tests/prefetch_related/tests.py b/tests/prefetch_related/tests.py index 1bb64c95fb..765223b71d 100644 --- a/tests/prefetch_related/tests.py +++ b/tests/prefetch_related/tests.py @@ -4,8 +4,10 @@ from django.core.exceptions import ObjectDoesNotExist from django.contrib.contenttypes.models import ContentType from django.db import connection from django.db.models import Prefetch +from django.db.models.query import get_prefetcher from django.test import TestCase, override_settings from django.utils import six +from django.utils.encoding import force_text from .models import (Author, Book, Reader, Qualification, Teacher, Department, TaggedItem, Bookmark, AuthorAddress, FavoriteAuthors, AuthorWithAge, @@ -1055,3 +1057,19 @@ class Ticket21410Tests(TestCase): def test_bug(self): list(Author2.objects.prefetch_related('first_book', 'favorite_books')) + + +class Ticket21760Tests(TestCase): + + def setUp(self): + self.rooms = [] + for _ in range(3): + house = House.objects.create() + for _ in range(3): + self.rooms.append(Room.objects.create(house = house)) + + #@override_settings(DEBUG=True) + def test_bug(self): + prefetcher = get_prefetcher(self.rooms[0], 'house')[0] + queryset = prefetcher.get_prefetch_queryset(list(Room.objects.all()))[0] + self.assertNotIn(' JOIN ', force_text(queryset.query))