|
|
d7da31 |
diff --git a/lib/sqlalchemy/sql/util.py b/lib/sqlalchemy/sql/util.py
|
|
|
d7da31 |
index 3077840..8719b26 100644
|
|
|
d7da31 |
--- a/lib/sqlalchemy/sql/util.py
|
|
|
d7da31 |
+++ b/lib/sqlalchemy/sql/util.py
|
|
|
d7da31 |
@@ -20,6 +20,7 @@ from .annotation import _shallow_annotate # noqa
|
|
|
d7da31 |
from .base import _from_objects
|
|
|
d7da31 |
from .base import ColumnSet
|
|
|
d7da31 |
from .ddl import sort_tables # noqa
|
|
|
d7da31 |
+from .elements import _expand_cloned
|
|
|
d7da31 |
from .elements import _find_columns # noqa
|
|
|
d7da31 |
from .elements import _label_reference
|
|
|
d7da31 |
from .elements import _textual_label_reference
|
|
|
d7da31 |
@@ -149,6 +150,16 @@ def find_left_clause_to_join_from(clauses, join_to, onclause):
|
|
|
d7da31 |
idx.append(i)
|
|
|
d7da31 |
break
|
|
|
d7da31 |
|
|
|
d7da31 |
+ if len(idx) > 1:
|
|
|
d7da31 |
+ # this is the same "hide froms" logic from
|
|
|
d7da31 |
+ # Selectable._get_display_froms
|
|
|
d7da31 |
+ toremove = set(
|
|
|
d7da31 |
+ chain(*[_expand_cloned(f._hide_froms) for f in clauses])
|
|
|
d7da31 |
+ )
|
|
|
d7da31 |
+ idx = [
|
|
|
d7da31 |
+ i for i in idx if clauses[i] not in toremove
|
|
|
d7da31 |
+ ]
|
|
|
d7da31 |
+
|
|
|
d7da31 |
# onclause was given and none of them resolved, so assume
|
|
|
d7da31 |
# all indexes can match
|
|
|
d7da31 |
if not idx and onclause is not None:
|
|
|
d7da31 |
diff --git a/test/orm/test_joins.py b/test/orm/test_joins.py
|
|
|
d7da31 |
index bb9747c..4fe04f7 100644
|
|
|
d7da31 |
--- a/test/orm/test_joins.py
|
|
|
d7da31 |
+++ b/test/orm/test_joins.py
|
|
|
d7da31 |
@@ -1559,6 +1559,81 @@ class JoinTest(QueryTest, AssertsCompiledSQL):
|
|
|
d7da31 |
"ON dingalings.address_id = addresses_1.id",
|
|
|
d7da31 |
)
|
|
|
d7da31 |
|
|
|
d7da31 |
+ def test_clause_present_in_froms_twice_w_onclause(self):
|
|
|
d7da31 |
+ # test [ticket:4584]
|
|
|
d7da31 |
+ Order, Address, Dingaling, User = (
|
|
|
d7da31 |
+ self.classes.Order,
|
|
|
d7da31 |
+ self.classes.Address,
|
|
|
d7da31 |
+ self.classes.Dingaling,
|
|
|
d7da31 |
+ self.classes.User,
|
|
|
d7da31 |
+ )
|
|
|
d7da31 |
+
|
|
|
d7da31 |
+ sess = create_session()
|
|
|
d7da31 |
+
|
|
|
d7da31 |
+ a1 = aliased(Address)
|
|
|
d7da31 |
+
|
|
|
d7da31 |
+ q = sess.query(Order).select_from(Order, a1, User)
|
|
|
d7da31 |
+ assert_raises_message(
|
|
|
d7da31 |
+ sa.exc.InvalidRequestError,
|
|
|
d7da31 |
+ "Can't determine which FROM clause to join from, there are "
|
|
|
d7da31 |
+ "multiple FROMS which can join to this entity. "
|
|
|
d7da31 |
+ "Try adding an explicit ON clause to help resolve the ambiguity.",
|
|
|
d7da31 |
+ q.outerjoin,
|
|
|
d7da31 |
+ a1,
|
|
|
d7da31 |
+ )
|
|
|
d7da31 |
+
|
|
|
d7da31 |
+ # the condition which occurs here is: Query._from_obj contains both
|
|
|
d7da31 |
+ # "a1" by itself as well as a join that "a1" is part of.
|
|
|
d7da31 |
+ # find_left_clause_to_join_from() needs to include removal of froms
|
|
|
d7da31 |
+ # that are in the _hide_froms of joins the same way
|
|
|
d7da31 |
+ # Selectable._get_display_froms does.
|
|
|
d7da31 |
+ q = sess.query(Order).select_from(Order, a1, User)
|
|
|
d7da31 |
+ q = q.outerjoin(a1, a1.id == Order.address_id)
|
|
|
d7da31 |
+ q = q.outerjoin(User, a1.user_id == User.id)
|
|
|
d7da31 |
+
|
|
|
d7da31 |
+ self.assert_compile(
|
|
|
d7da31 |
+ q,
|
|
|
d7da31 |
+ "SELECT orders.id AS orders_id, orders.user_id AS orders_user_id, "
|
|
|
d7da31 |
+ "orders.address_id AS orders_address_id, "
|
|
|
d7da31 |
+ "orders.description AS orders_description, "
|
|
|
d7da31 |
+ "orders.isopen AS orders_isopen "
|
|
|
d7da31 |
+ "FROM orders "
|
|
|
d7da31 |
+ "LEFT OUTER JOIN addresses AS addresses_1 "
|
|
|
d7da31 |
+ "ON addresses_1.id = orders.address_id "
|
|
|
d7da31 |
+ "LEFT OUTER JOIN users ON addresses_1.user_id = users.id",
|
|
|
d7da31 |
+ )
|
|
|
d7da31 |
+
|
|
|
d7da31 |
+ def test_clause_present_in_froms_twice_wo_onclause(self):
|
|
|
d7da31 |
+ # test [ticket:4584]
|
|
|
d7da31 |
+ Order, Address, Dingaling, User = (
|
|
|
d7da31 |
+ self.classes.Order,
|
|
|
d7da31 |
+ self.classes.Address,
|
|
|
d7da31 |
+ self.classes.Dingaling,
|
|
|
d7da31 |
+ self.classes.User,
|
|
|
d7da31 |
+ )
|
|
|
d7da31 |
+
|
|
|
d7da31 |
+ sess = create_session()
|
|
|
d7da31 |
+
|
|
|
d7da31 |
+ a1 = aliased(Address)
|
|
|
d7da31 |
+
|
|
|
d7da31 |
+ # the condition which occurs here is: Query._from_obj contains both
|
|
|
d7da31 |
+ # "a1" by itself as well as a join that "a1" is part of.
|
|
|
d7da31 |
+ # find_left_clause_to_join_from() needs to include removal of froms
|
|
|
d7da31 |
+ # that are in the _hide_froms of joins the same way
|
|
|
d7da31 |
+ # Selectable._get_display_froms does.
|
|
|
d7da31 |
+ q = sess.query(User).select_from(Dingaling, a1, User)
|
|
|
d7da31 |
+ q = q.outerjoin(a1, User.id == a1.user_id)
|
|
|
d7da31 |
+ q = q.outerjoin(Dingaling)
|
|
|
d7da31 |
+
|
|
|
d7da31 |
+ self.assert_compile(
|
|
|
d7da31 |
+ q,
|
|
|
d7da31 |
+ "SELECT users.id AS users_id, users.name AS users_name "
|
|
|
d7da31 |
+ "FROM users LEFT OUTER JOIN addresses AS addresses_1 "
|
|
|
d7da31 |
+ "ON users.id = addresses_1.user_id "
|
|
|
d7da31 |
+ "LEFT OUTER JOIN dingalings "
|
|
|
d7da31 |
+ "ON addresses_1.id = dingalings.address_id",
|
|
|
d7da31 |
+ )
|
|
|
d7da31 |
+
|
|
|
d7da31 |
def test_multiple_adaption(self):
|
|
|
d7da31 |
Item, Order, User = (
|
|
|
d7da31 |
self.classes.Item,
|