Commits

Anonymous committed 06c1bd5

queryset-refactor: Removed an unwanted side-effect from the Query.as_sql() method.

Fixed #7056

Comments (0)

Files changed (1)

django/db/models/sql/query.py

 all about the internals of models in order to get the information it needs.
 """
 
-import itertools
 from copy import deepcopy
 
 from django.utils.tree import Node
         self.low_mark, self.high_mark = 0, None  # Used for offset/limit
         self.distinct = False
         self.select_related = False
+        self.related_select_cols = []
 
         # Arbitrary maximum limit for select_related. Prevents infinite
         # recursion. Can be changed by the depth parameter to select_related().
         obj.low_mark, obj.high_mark = self.low_mark, self.high_mark
         obj.distinct = self.distinct
         obj.select_related = self.select_related
+        obj.related_select_cols = []
         obj.max_depth = self.max_depth
         obj.extra_select = self.extra_select.copy()
         obj.extra_select_params = self.extra_select_params
         obj.clear_ordering(True)
         obj.clear_limits()
         obj.select_related = False
+        obj.related_select_cols = []
         if obj.distinct and len(obj.select) > 1:
             obj = self.clone(CountQuery, _query=obj, where=self.where_class(),
                     distinct=False)
         """
         if not self.tables:
             self.join((None, self.model._meta.db_table, None, None))
-        if self.select_related:
+        if self.select_related and not self.related_select_cols:
             self.fill_related_selections()
 
     def get_columns(self):
             cols = self.get_default_columns(True)
             result.extend(cols)
             aliases.extend(cols)
+        for table, col in self.related_select_cols:
+            r = '%s.%s' % (qn(table), qn(col))
+            result.append(r)
+            aliases.append(r)
 
         self._select_aliases = set(aliases)
         return result
         if not opts:
             opts = self.get_meta()
             root_alias = self.get_initial_alias()
-            self.select.extend(self.get_default_columns())
+            self.related_select_cols = []
         if not used:
             used = set()
 
                     f.rel.get_related_field().column), exclusions=used,
                     promote=f.null)
             used.add(alias)
-            self.select.extend([(alias, f2.column)
+            self.related_select_cols.extend([(alias, f2.column)
                     for f2 in f.rel.to._meta.fields])
             if restricted:
                 next = requested.get(f.name, {})
             for part in field.split(LOOKUP_SEP):
                 d = d.setdefault(part, {})
         self.select_related = field_dict
+        self.related_select_cols = []
 
     def add_extra(self, select, select_params, where, params, tables, order_by):
         """