summaryrefslogtreecommitdiff
path: root/django/db/models/sql
diff options
context:
space:
mode:
authorAlex Gaynor <alex.gaynor@gmail.com>2010-11-26 14:24:08 +0000
committerAlex Gaynor <alex.gaynor@gmail.com>2010-11-26 14:24:08 +0000
commit678f626c24f389b03d4bfe2c552c33e9bdcc9379 (patch)
treed98434d8ebc96ef7b191a12091f3a8268609ea0e /django/db/models/sql
parent3cbaf3c2b66ada38bb1ca01b6404350ef5345f9e (diff)
Fixed a suite of errors in the ORM -- a) fixed calling values_list().values_list() and changing whether the results are flat, b) fixed an issue with fields on the left-hand side of what becomes the HAVING clause not being included in the GROUP BY clause, and c) fixed a bug with fields from values() calls not being included in the GROUP BY clause. This fixed the recent test failures under postgresql.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@14715 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/db/models/sql')
-rw-r--r--django/db/models/sql/compiler.py14
-rw-r--r--django/db/models/sql/query.py14
-rw-r--r--django/db/models/sql/subqueries.py2
3 files changed, 22 insertions, 8 deletions
diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py
index b2249dc607..ba707d4140 100644
--- a/django/db/models/sql/compiler.py
+++ b/django/db/models/sql/compiler.py
@@ -469,9 +469,11 @@ class SQLCompiler(object):
qn = self.quote_name_unless_alias
result, params = [], []
if self.query.group_by is not None:
- if len(self.query.model._meta.fields) == len(self.query.select) and \
- self.connection.features.allows_group_by_pk:
- self.query.group_by = [(self.query.model._meta.db_table, self.query.model._meta.pk.column)]
+ if (len(self.query.model._meta.fields) == len(self.query.select) and
+ self.connection.features.allows_group_by_pk):
+ self.query.group_by = [
+ (self.query.model._meta.db_table, self.query.model._meta.pk.column)
+ ]
group_by = self.query.group_by or []
@@ -479,11 +481,13 @@ class SQLCompiler(object):
for extra_select, extra_params in self.query.extra_select.itervalues():
extra_selects.append(extra_select)
params.extend(extra_params)
- for col in group_by + self.query.related_select_cols + extra_selects:
+ cols = (group_by + self.query.select +
+ self.query.related_select_cols + extra_selects)
+ for col in cols:
if isinstance(col, (list, tuple)):
result.append('%s.%s' % (qn(col[0]), qn(col[1])))
elif hasattr(col, 'as_sql'):
- result.append(col.as_sql(qn))
+ result.append(col.as_sql(qn, self.connection))
else:
result.append('(%s)' % str(col))
return result, params
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index b8638fe909..eae7a87ac7 100644
--- a/django/db/models/sql/query.py
+++ b/django/db/models/sql/query.py
@@ -195,8 +195,9 @@ class Query(object):
Unpickling support.
"""
# Rebuild list of field instances
+ opts = obj_dict['model']._meta
obj_dict['select_fields'] = [
- name is not None and obj_dict['model']._meta.get_field(name) or None
+ name is not None and opts.get_field(name) or None
for name in obj_dict['select_fields']
]
@@ -707,13 +708,20 @@ class Query(object):
# "group by", "where" and "having".
self.where.relabel_aliases(change_map)
self.having.relabel_aliases(change_map)
- for columns in (self.select, self.aggregates.values(), self.group_by or []):
+ for columns in [self.select, self.group_by or []]:
for pos, col in enumerate(columns):
if isinstance(col, (list, tuple)):
old_alias = col[0]
columns[pos] = (change_map.get(old_alias, old_alias), col[1])
else:
col.relabel_aliases(change_map)
+ for mapping in [self.aggregates]:
+ for key, col in mapping.items():
+ if isinstance(col, (list, tuple)):
+ old_alias = col[0]
+ mapping[key] = (change_map.get(old_alias, old_alias), col[1])
+ else:
+ col.relabel_aliases(change_map)
# 2. Rename the alias in the internal table/alias datastructures.
for old_alias, new_alias in change_map.iteritems():
@@ -1075,6 +1083,8 @@ class Query(object):
if having_clause:
+ if (alias, col) not in self.group_by:
+ self.group_by.append((alias, col))
self.having.add((Constraint(alias, col, field), lookup_type, value),
connector)
else:
diff --git a/django/db/models/sql/subqueries.py b/django/db/models/sql/subqueries.py
index 003bf432c0..bb4b838b82 100644
--- a/django/db/models/sql/subqueries.py
+++ b/django/db/models/sql/subqueries.py
@@ -202,7 +202,7 @@ class DateQuery(Query):
alias = result[3][-1]
select = Date((alias, field.column), lookup_type)
self.select = [select]
- self.select_fields = [None]
+ self.select_fields = []
self.select_related = False # See #7097.
self.set_extra_mask([])
self.distinct = True