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