Provide more detailed error message for Query.join()

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: b148df5470

Change-Id: I873453d1fdb651178216aac698baac63ae5a94e8
This commit is contained in:
RamonWill
2020-08-06 16:08:13 -04:00
committed by Mike Bayer
parent 0a234fee4b
commit 2cc963dbdc
3 changed files with 34 additions and 1 deletions
+8
View File
@@ -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.
+12 -1
View 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
+14
View 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()."""