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