# Buggy code
def _join_determine_implicit_left_side(self, left, right, onclause):
    """When join conditions don't express the left side explicitly,
    determine if an existing FROM or entity in this query
    can serve as the left hand side.

    """

    # when we are here, it means join() was called without an ORM-
    # specific way of telling us what the "left" side is, e.g.:
    #
    # join(RightEntity)
    #
    # or
    #
    # join(RightEntity, RightEntity.foo == LeftEntity.bar)
    #

    r_info = inspect(right)

    replace_from_obj_index = use_entity_index = None

    if self._from_obj:
        # we have a list of FROMs already.  So by definition this
        # join has to connect to one of those FROMs.

        indexes = sql_util.find_left_clause_to_join_from(
            self._from_obj, r_info.selectable, onclause
        )

        if len(indexes) == 1:
            replace_from_obj_index = indexes[0]
            left = self._from_obj[replace_from_obj_index]
        elif len(indexes) > 1:
            raise sa_exc.InvalidRequestError(
                "Can't determine which FROM clause to join "
                "from, there are multiple FROMS which can "
                "join to this entity. Try adding an explicit ON clause "
                "to help resolve the ambiguity."
            )
        else:
            raise sa_exc.InvalidRequestError(
                "Don't know how to join to %s; please use "
                "an ON clause to more clearly establish the left "
                "side of this join" % (right,)
            )

    elif self._entities:
        # we have no explicit FROMs, so the implicit left has to
        # come from our list of entities.

        potential = {}
        for entity_index, ent in enumerate(self._entities):
            entity = ent.entity_zero_or_selectable
            if entity is None:
                continue
            ent_info = inspect(entity)
            if ent_info is r_info:  # left and right are the same, skip
                continue

            # by using a dictionary with the selectables as keys this
            # de-duplicates those selectables as occurs when the query is
            # against a series of columns from the same selectable
            if isinstance(ent, _MapperEntity):
                potential[ent.selectable] = (entity_index, entity)
            else:
                potential[ent_info.selectable] = (None, entity)

        all_clauses = list(potential.keys())
        indexes = sql_util.find_left_clause_to_join_from(
            all_clauses, r_info.selectable, onclause
        )

        if len(indexes) == 1:
            use_entity_index, left = potential[all_clauses[indexes[0]]]
        elif len(indexes) > 1:
            raise sa_exc.InvalidRequestError(
                "Can't determine which FROM clause to join "
                "from, there are multiple FROMS which can "
                "join to this entity. Try adding an explicit ON clause "
                "to help resolve the ambiguity."
            )
        else:
            raise sa_exc.InvalidRequestError(
                "Don't know how to join to %s; please use "
                "an ON clause to more clearly establish the left "
                "side of this join" % (right,)
            )
    else:
        raise sa_exc.InvalidRequestError(
            "No entities to join from; please use "
            "select_from() to establish the left "
            "entity/selectable of this join"
        )

    return left, replace_from_obj_index, use_entity_index


# Traceback (sqlalchemy.exc.InvalidRequestError)
Traceback (most recent call last):
File "/home/mahenzon/.PyCharm2019.2/config/scratches/scratch_32.py", line 283, in <module>
main()
File "/home/mahenzon/.PyCharm2019.2/config/scratches/scratch_32.py", line 279, in main
run_cte_query()
File "/home/mahenzon/.PyCharm2019.2/config/scratches/scratch_32.py", line 269, in run_cte_query
query = create_cte_query()
File "/home/mahenzon/.PyCharm2019.2/config/scratches/scratch_32.py", line 258, in create_cte_query
full=True,
File "/home/mahenzon/.local/share/virtualenvs/core-C3GrfXXb/src/sqlalchemy/lib/sqlalchemy/orm/query.py", line 2246, in join
from_joinpoint=from_joinpoint,
File "<string>", line 2, in _join
File "/home/mahenzon/.local/share/virtualenvs/core-C3GrfXXb/src/sqlalchemy/lib/sqlalchemy/orm/base.py", line 220, in generate
fn(self, *args[1:], **kw)
File "/home/mahenzon/.local/share/virtualenvs/core-C3GrfXXb/src/sqlalchemy/lib/sqlalchemy/orm/query.py", line 2425, in _join
left, right, onclause, prop, create_aliases, outerjoin, full
File "/home/mahenzon/.local/share/virtualenvs/core-C3GrfXXb/src/sqlalchemy/lib/sqlalchemy/orm/query.py", line 2448, in _join_left_to_right
) = self._join_determine_implicit_left_side(left, right, onclause)
File "/home/mahenzon/.local/share/virtualenvs/core-C3GrfXXb/src/sqlalchemy/lib/sqlalchemy/orm/query.py", line 2590, in _join_determine_implicit_left_side
"side of this join" % (right,)
sqlalchemy.exc.InvalidRequestError: Don't know how to join to ; please use an ON clause to more clearly establish the left side of this join

# Fixed code
def _join_determine_implicit_left_side(self, left, right, onclause):
    """When join conditions don't express the left side explicitly,
    determine if an existing FROM or entity in this query
    can serve as the left hand side.

    """

    # when we are here, it means join() was called without an ORM-
    # specific way of telling us what the "left" side is, e.g.:
    #
    # join(RightEntity)
    #
    # or
    #
    # join(RightEntity, RightEntity.foo == LeftEntity.bar)
    #

    r_info = inspect(right)

    replace_from_obj_index = use_entity_index = None

    if self._from_obj:
        # we have a list of FROMs already.  So by definition this
        # join has to connect to one of those FROMs.

        indexes = sql_util.find_left_clause_to_join_from(
            self._from_obj, r_info.selectable, onclause
        )

        if len(indexes) == 1:
            replace_from_obj_index = indexes[0]
            left = self._from_obj[replace_from_obj_index]
        elif len(indexes) > 1:
            raise sa_exc.InvalidRequestError(
                "Can't determine which FROM clause to join "
                "from, there are multiple FROMS which can "
                "join to this entity. Please use the .select_from() "
                "method to establish an explicit left side, as well as "
                "providing an explcit ON clause if not present already to "
                "help resolve the ambiguity."
            )
        else:
            raise sa_exc.InvalidRequestError(
                "Don't know how to join to %r. "
                "Please use the .select_from() "
                "method to establish an explicit left side, as well as "
                "providing an explcit ON clause if not present already to "
                "help resolve the ambiguity." % (right,)
            )

    elif self._entities:
        # we have no explicit FROMs, so the implicit left has to
        # come from our list of entities.

        potential = {}
        for entity_index, ent in enumerate(self._entities):
            entity = ent.entity_zero_or_selectable
            if entity is None:
                continue
            ent_info = inspect(entity)
            if ent_info is r_info:  # left and right are the same, skip
                continue

            # by using a dictionary with the selectables as keys this
            # de-duplicates those selectables as occurs when the query is
            # against a series of columns from the same selectable
            if isinstance(ent, _MapperEntity):
                potential[ent.selectable] = (entity_index, entity)
            else:
                potential[ent_info.selectable] = (None, entity)

        all_clauses = list(potential.keys())
        indexes = sql_util.find_left_clause_to_join_from(
            all_clauses, r_info.selectable, onclause
        )

        if len(indexes) == 1:
            use_entity_index, left = potential[all_clauses[indexes[0]]]
        elif len(indexes) > 1:
            raise sa_exc.InvalidRequestError(
                "Can't determine which FROM clause to join "
                "from, there are multiple FROMS which can "
                "join to this entity. Please use the .select_from() "
                "method to establish an explicit left side, as well as "
                "providing an explcit ON clause if not present already to "
                "help resolve the ambiguity."
            )
        else:
            raise sa_exc.InvalidRequestError(
                "Don't know how to join to %r. "
                "Please use the .select_from() "
                "method to establish an explicit left side, as well as "
                "providing an explcit ON clause if not present already to "
                "help resolve the ambiguity." % (right,)
            )
    else:
        raise sa_exc.InvalidRequestError(
            "No entities to join from; please use "
            "select_from() to establish the left "
            "entity/selectable of this join"
        )

    return left, replace_from_obj_index, use_entity_index