From 3697ddbf7526e85483067db5a2fde93f661da360 Mon Sep 17 00:00:00 2001 From: James Timmins Date: Thu, 19 Sep 2019 19:20:40 -0700 Subject: [3.0.x] Fixed #30771 -- Fixed exact lookup against queries with selected columns. Use pre-existing select fields (and thereby GROUP BY fields) from subquery if they were specified, instead of always defaulting to pk. Thanks Aur Saraf for the report and Simon Charette for guidance. Backport of 0719edcd5fed56157ffb3323a8f634aa5e8f9a80 from master --- django/db/models/lookups.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'django') diff --git a/django/db/models/lookups.py b/django/db/models/lookups.py index 105dc93251..2683d8971a 100644 --- a/django/db/models/lookups.py +++ b/django/db/models/lookups.py @@ -262,9 +262,9 @@ class Exact(FieldGetDbPrepValueMixin, BuiltinLookup): from django.db.models.sql.query import Query if isinstance(self.rhs, Query): if self.rhs.has_limit_one(): - # The subquery must select only the pk. - self.rhs.clear_select_clause() - self.rhs.add_fields(['pk']) + if not self.rhs.has_select_fields: + self.rhs.clear_select_clause() + self.rhs.add_fields(['pk']) else: raise ValueError( 'The QuerySet value for an exact lookup must be limited to ' -- cgit v1.3