diff options
author | Luke Cyca <me@lukecyca.com> | 2013-03-07 11:56:11 -0800 |
---|---|---|
committer | Luke Cyca <me@lukecyca.com> | 2013-03-07 11:56:11 -0800 |
commit | f122a307e03d9a1f2322b35429972a5f928d5b30 (patch) | |
tree | 7eed4eee764516a8310ac5bf6e3a78639b6cb295 /lib/sqlalchemy/sql/expression.py | |
parent | 3b7fbdb2962439438c38e972843d3c8ef05eb216 (diff) | |
download | sqlalchemy-f122a307e03d9a1f2322b35429972a5f928d5b30.tar.gz |
Changed behavior of Select.correlate() to ignore correlations to froms that don't exist in the superquery.
Diffstat (limited to 'lib/sqlalchemy/sql/expression.py')
-rw-r--r-- | lib/sqlalchemy/sql/expression.py | 59 |
1 files changed, 34 insertions, 25 deletions
diff --git a/lib/sqlalchemy/sql/expression.py b/lib/sqlalchemy/sql/expression.py index 490004e39..0ebcc1146 100644 --- a/lib/sqlalchemy/sql/expression.py +++ b/lib/sqlalchemy/sql/expression.py @@ -4980,7 +4980,7 @@ class CompoundSelect(SelectBase): INTERSECT_ALL = util.symbol('INTERSECT ALL') def __init__(self, keyword, *selects, **kwargs): - self._should_correlate = kwargs.pop('correlate', False) + self._auto_correlate = kwargs.pop('correlate', False) self.keyword = keyword self.selects = [] @@ -5159,7 +5159,7 @@ class Select(HasPrefixes, SelectBase): :class:`SelectBase` superclass. """ - self._should_correlate = correlate + self._auto_correlate = correlate if distinct is not False: if distinct is True: self._distinct = True @@ -5232,7 +5232,7 @@ class Select(HasPrefixes, SelectBase): return froms - def _get_display_froms(self, existing_froms=None): + def _get_display_froms(self, existing_froms=None, asfrom=False): """Return the full list of 'from' clauses to be displayed. Takes into account a set of existing froms which may be @@ -5258,25 +5258,34 @@ class Select(HasPrefixes, SelectBase): # using a list to maintain ordering froms = [f for f in froms if f not in toremove] - if len(froms) > 1 or self._correlate or self._correlate_except: - if self._correlate: - froms = [f for f in froms if f not in - _cloned_intersection(froms, - self._correlate)] - if self._correlate_except: - froms = [f for f in froms if f in _cloned_intersection(froms, - self._correlate_except)] - if self._should_correlate and existing_froms: - froms = [f for f in froms if f not in - _cloned_intersection(froms, - existing_froms)] - - if not len(froms): - raise exc.InvalidRequestError("Select statement '%s" - "' returned no FROM clauses due to " - "auto-correlation; specify " - "correlate(<tables>) to control " - "correlation manually." % self) + if self._correlate: + froms = [ + f for f in froms if f not in + _cloned_intersection( + _cloned_intersection(froms, existing_froms or ()), + self._correlate + ) + ] + if self._correlate_except: + froms = [ + f for f in froms if f in + _cloned_intersection( + froms, + self._correlate_except + ) + ] + if self._auto_correlate and existing_froms and len(froms) > 1 and not asfrom: + froms = [ + f for f in froms if f not in + _cloned_intersection(froms, existing_froms) + ] + + if not len(froms): + raise exc.InvalidRequestError("Select statement '%s" + "' returned no FROM clauses due to " + "auto-correlation; specify " + "correlate(<tables>) to control " + "correlation manually." % self) return froms @@ -5642,7 +5651,7 @@ class Select(HasPrefixes, SelectBase): :ref:`correlated_subqueries` """ - self._should_correlate = False + self._auto_correlate = False if fromclauses and fromclauses[0] is None: self._correlate = () else: @@ -5662,7 +5671,7 @@ class Select(HasPrefixes, SelectBase): :ref:`correlated_subqueries` """ - self._should_correlate = False + self._auto_correlate = False if fromclauses and fromclauses[0] is None: self._correlate_except = () else: @@ -5673,7 +5682,7 @@ class Select(HasPrefixes, SelectBase): """append the given correlation expression to this select() construct.""" - self._should_correlate = False + self._auto_correlate = False self._correlate = set(self._correlate).union( _interpret_as_from(f) for f in fromclause) |