]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
resolves #4428
authorRamonWill <ramonwilliams@hotmail.co.uk>
Sun, 12 Jul 2020 20:34:09 +0000 (21:34 +0100)
committerRamonWill <ramonwilliams@hotmail.co.uk>
Sun, 12 Jul 2020 20:34:09 +0000 (21:34 +0100)
doc/build/changelog/unreleased_14/4428.rst [new file with mode: 0644]
lib/sqlalchemy/orm/context.py
test/orm/test_joins.py

diff --git a/doc/build/changelog/unreleased_14/4428.rst b/doc/build/changelog/unreleased_14/4428.rst
new file mode 100644 (file)
index 0000000..aaef0c9
--- /dev/null
@@ -0,0 +1,8 @@
+.. change::
+    :tags: bug, fairly easy, orm
+    :tickets: 4428
+
+    An :class:`.ArgumentError` with more detail is now raised if the target
+    parameter for :meth:`_query.Query.join` is set to an unmapped object.
+    Prior to this change a less detailed ``AttributeError`` was raised.
+    Pull request courtesy Ramon Williams.
index 55a6b4cd2132454d6c7c230b5ea4dc51bee24bfb..564845e7dee68d397af8481a28e390b91c11f81c 100644 (file)
@@ -1292,7 +1292,15 @@ class ORMSelectCompileState(ORMCompileState, SelectState):
                     if of_type:
                         right = of_type
                     else:
-                        right = onclause.property.entity
+                        right = onclause.property
+
+                        try:
+                            right = right.entity
+                        except AttributeError as err:
+                            raise sa_exc.ArgumentError(
+                                "Join target %s does not refer to a "
+                                "mapped entity" % right
+                            ) from err
 
                 left = onclause._parententity
 
index 4ffa5fb9e9c37a7f5e7e6eafc5741dc9489f37ed..9b573ec1e94702ada397e471760350e77339303c 100644 (file)
@@ -2231,6 +2231,19 @@ class JoinTest(QueryTest, AssertsCompiledSQL):
             sess.query(User).join(User.id == Address.user_id)._compile_context,
         )
 
+    def test_on_clause_no_right_side_two(self):
+        User = self.classes.User
+        Address = self.classes.Address
+        sess = create_session()
+
+        right = Address.user_id
+
+        assert_raises_message(
+            sa_exc.ArgumentError,
+            "Join target %s does not refer to a mapped entity" % right,
+            sess.query(User).join(Address.user_id)._compile_context,
+        )
+
     def test_select_from(self):
         """Test that the left edge of the join can be set reliably with
         select_from()."""