]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Fix regression for self-ref join to self error message
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 18 Jul 2019 15:12:49 +0000 (11:12 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 18 Jul 2019 15:14:19 +0000 (11:14 -0400)
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

doc/build/changelog/unreleased_13/4773.rst [new file with mode: 0644]
lib/sqlalchemy/orm/query.py
test/orm/test_joins.py

diff --git a/doc/build/changelog/unreleased_13/4773.rst b/doc/build/changelog/unreleased_13/4773.rst
new file mode 100644 (file)
index 0000000..8ac9e42
--- /dev/null
@@ -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.
+
index b5c49ee05f67e900bfad1b1d2b129bd96037762e..4b176cb55891a8e3b3d0f386484f234529ba6be1 100644 (file)
@@ -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
index 24c2b6d1b145534f47f719a36eebe632c0c07dfa..1243c7f19432a86dbbda9b056c0c243c1b278a25 100644 (file)
@@ -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)