summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/orm
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2016-02-09 17:49:38 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2016-02-09 17:49:38 -0500
commitff3be95620b6505943b2d7e4688abc29dca3e493 (patch)
tree1d90206b004c30bc296d709d5d169bf8a1f2a16a /lib/sqlalchemy/orm
parent7d2bed69abb6ab545cfa5ca967141338387417c2 (diff)
downloadsqlalchemy-ff3be95620b6505943b2d7e4688abc29dca3e493.tar.gz
- A refinement to the logic which adds columns to the resulting SQL when
:meth:`.Query.distinct` is combined with :meth:`.Query.order_by` such that columns which are already present will not be added a second time, even if they are labeled with a different name. Regardless of this change, the extra columns added to the SQL have never been returned in the final result, so this change only impacts the string form of the statement as well as its behavior when used in a Core execution context. Additionally, columns are no longer added when the DISTINCT ON format is used, provided the query is not wrapped inside a subquery due to joined eager loading. fixes #3641
Diffstat (limited to 'lib/sqlalchemy/orm')
-rw-r--r--lib/sqlalchemy/orm/query.py26
1 files changed, 11 insertions, 15 deletions
diff --git a/lib/sqlalchemy/orm/query.py b/lib/sqlalchemy/orm/query.py
index 08600c357..8a25f570a 100644
--- a/lib/sqlalchemy/orm/query.py
+++ b/lib/sqlalchemy/orm/query.py
@@ -3254,12 +3254,11 @@ class Query(object):
# then append eager joins onto that
if context.order_by:
- order_by_col_expr = list(
- chain(*[
- sql_util.unwrap_order_by(o)
- for o in context.order_by
- ])
- )
+ order_by_col_expr = \
+ sql_util.expand_column_list_from_order_by(
+ context.primary_columns,
+ context.order_by
+ )
else:
context.order_by = None
order_by_col_expr = []
@@ -3319,15 +3318,12 @@ class Query(object):
if not context.order_by:
context.order_by = None
- if self._distinct and context.order_by:
- order_by_col_expr = list(
- chain(*[
- sql_util.unwrap_order_by(o)
- for o in context.order_by
- ])
- )
- context.primary_columns += order_by_col_expr
-
+ if self._distinct is True and context.order_by:
+ context.primary_columns += \
+ sql_util.expand_column_list_from_order_by(
+ context.primary_columns,
+ context.order_by
+ )
context.froms += tuple(context.eager_joins.values())
statement = sql.select(