]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Provide more detailed error message for Query.join()
authorRamonWill <ramonwilliams@hotmail.co.uk>
Thu, 6 Aug 2020 20:08:13 +0000 (16:08 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 31 Aug 2020 22:27:48 +0000 (18:27 -0400)
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.

Fixes: #4428
Closes: #5452
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/5452
Pull-request-sha: b148df547037e9a254fe331eff8e922c78426261

Change-Id: I873453d1fdb651178216aac698baac63ae5a94e8

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

diff --git a/doc/build/changelog/unreleased_13/4428.rst b/doc/build/changelog/unreleased_13/4428.rst
new file mode 100644 (file)
index 0000000..e677669
--- /dev/null
@@ -0,0 +1,8 @@
+.. change::
+    :tags: bug, 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 c62f6a90252684bb98c9cf2d19ae71a128a56a0e..8f4b3b0aca367be7b016ba95c333c52c333a3277 100644 (file)
@@ -2512,7 +2512,18 @@ class Query(object):
                     if of_type:
                         right = of_type
                     else:
-                        right = onclause.property.entity
+                        right = onclause.property
+
+                        try:
+                            right = right.entity
+                        except AttributeError as err:
+                            util.raise_(
+                                sa_exc.ArgumentError(
+                                    "Join target %s does not refer to a "
+                                    "mapped entity" % right
+                                ),
+                                replace_context=err,
+                            )
 
                 left = onclause._parententity
 
index aaa96ae4ad8cf1b7e2416eeb9729a5832f89f583..845c6e6f246fd414d2e2b9719e0136380feda6b8 100644 (file)
@@ -2400,6 +2400,20 @@ class JoinTest(QueryTest, AssertsCompiledSQL):
             User.id == Address.user_id,
         )
 
+    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,
+        )
+
     def test_select_from(self):
         """Test that the left edge of the join can be set reliably with
         select_from()."""