summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/orm/query.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2012-07-28 17:12:09 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2012-07-28 17:12:09 -0400
commitf839b8927099b64b2d120ffd93d5f444b8951e59 (patch)
treec4e6c15d586f0ddd4d76753f0427285e1a3ceb4d /lib/sqlalchemy/orm/query.py
parent22ba1c43b792953ae6f791512d276739c8c09eae (diff)
downloadsqlalchemy-f839b8927099b64b2d120ffd93d5f444b8951e59.tar.gz
- [feature] Added reduce_columns() method
to select() construct, replaces columns inline using the util.reduce_columns utility function to remove equivalent columns. reduce_columns() also adds "with_only_synonyms" to limit the reduction just to those columns which have the same name. The deprecated fold_equivalents() feature is removed [ticket:1729]. - [feature] Added with_labels and reduce_columns keyword arguments to Query.subquery(), to provide two alternate strategies for producing queries with uniquely- named columns. [ticket:1729].
Diffstat (limited to 'lib/sqlalchemy/orm/query.py')
-rw-r--r--lib/sqlalchemy/orm/query.py26
1 files changed, 19 insertions, 7 deletions
diff --git a/lib/sqlalchemy/orm/query.py b/lib/sqlalchemy/orm/query.py
index 0a345f284..46ee59298 100644
--- a/lib/sqlalchemy/orm/query.py
+++ b/lib/sqlalchemy/orm/query.py
@@ -434,25 +434,37 @@ class Query(object):
# the annotation not being there
return stmt._annotate({'no_replacement_traverse': True})
- def subquery(self, name=None):
+ def subquery(self, name=None, with_labels=False, reduce_columns=False):
"""return the full SELECT statement represented by
this :class:`.Query`, embedded within an :class:`.Alias`.
Eager JOIN generation within the query is disabled.
- The statement will not have disambiguating labels
- applied to the list of selected columns unless the
- :meth:`.Query.with_labels` method is used to generate a new
- :class:`.Query` with the option enabled.
-
:param name: string name to be assigned as the alias;
this is passed through to :meth:`.FromClause.alias`.
If ``None``, a name will be deterministically generated
at compile time.
+ :param with_labels: if True, :meth:`.with_labels` will be called
+ on the :class:`.Query` first to apply table-qualified labels
+ to all columns.
+
+ :param reduce_columns: if True, :meth:`.Select.reduce_columns` will
+ be called on the resulting :func:`.select` construct,
+ to remove same-named columns where one also refers to the other
+ via foreign key or WHERE clause equivalence.
+
+ .. versionchanged:: 0.8 the ``with_labels`` and ``reduce_columns``
+ keyword arguments were added.
"""
- return self.enable_eagerloads(False).statement.alias(name=name)
+ q = self.enable_eagerloads(False)
+ if with_labels:
+ q = q.with_labels()
+ q = q.statement
+ if reduce_columns:
+ q = q.reduce_columns()
+ return q.alias(name=name)
def cte(self, name=None, recursive=False):
"""Return the full SELECT statement represented by this