From: Mike Bayer Date: Thu, 18 Jul 2019 15:12:49 +0000 (-0400) Subject: Fix regression for self-ref join to self error message X-Git-Tag: rel_1_4_0b1~792 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=736186f840646773217d3f7ba3f6a5d0bbfe1fdb;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Fix regression for self-ref join to self error message Fixed regression caused by :ticket:`4365` where a join from an entity to itself without using aliases no longer raises an informative error message, instead failing on an assertion. The informative error condition has been restored. Fixes: #4773 Change-Id: Iabeb56f3f0c2a40e350fd7c69f07c02dc9804c2c --- diff --git a/doc/build/changelog/unreleased_13/4773.rst b/doc/build/changelog/unreleased_13/4773.rst new file mode 100644 index 0000000000..8ac9e42fb5 --- /dev/null +++ b/doc/build/changelog/unreleased_13/4773.rst @@ -0,0 +1,9 @@ +.. change:: + :tags: bug, orm + :tickets: 4773 + + Fixed regression caused by :ticket:`4365` where a join from an entity to + itself without using aliases no longer raises an informative error message, + instead failing on an assertion. The informative error condition has been + restored. + diff --git a/lib/sqlalchemy/orm/query.py b/lib/sqlalchemy/orm/query.py index b5c49ee05f..4b176cb558 100644 --- a/lib/sqlalchemy/orm/query.py +++ b/lib/sqlalchemy/orm/query.py @@ -2467,9 +2467,11 @@ class Query(object): use_entity_index, ) = self._join_place_explicit_left_side(left) - # this should never happen because we would not have found a place - # to join on - assert left is not right or create_aliases + if left is right and not create_aliases: + raise sa_exc.InvalidRequestError( + "Can't construct a join from %s to %s, they " + "are the same entity" % (left, right) + ) # the right side as given often needs to be adapted. additionally # a lot of things can be wrong with it. handle all that and diff --git a/test/orm/test_joins.py b/test/orm/test_joins.py index 24c2b6d1b1..1243c7f194 100644 --- a/test/orm/test_joins.py +++ b/test/orm/test_joins.py @@ -1175,8 +1175,13 @@ class JoinTest(QueryTest, AssertsCompiledSQL): ) self.assert_compile( - join(User, oalias2, User.id == oalias2.user_id, - isouter=True, full=True), + join( + User, + oalias2, + User.id == oalias2.user_id, + isouter=True, + full=True, + ), "users FULL OUTER JOIN orders AS orders_1 " "ON users.id = orders_1.user_id", use_default_dialect=True, @@ -3370,6 +3375,18 @@ class SelfReferentialTest(fixtures.MappedTest, AssertsCompiledSQL): use_default_dialect=True, ) + def test_join_to_self_no_aliases_raises(self): + Node = self.classes.Node + + s = Session() + assert_raises_message( + sa.exc.InvalidRequestError, + "Can't construct a join from mapped class Node->nodes to mapped " + "class Node->nodes, they are the same entity", + s.query(Node).join, + Node.children, + ) + def test_explicit_join_1(self): Node = self.classes.Node n1 = aliased(Node)