From 17090c004e19afab35c837bf880ea5b328e1fb56 Mon Sep 17 00:00:00 2001 From: RamonWill Date: Mon, 24 Aug 2020 20:14:15 -0400 Subject: Provide a 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: b148df547037e9a254fe331eff8e922c78426261 Change-Id: I873453d1fdb651178216aac698baac63ae5a94e8 --- lib/sqlalchemy/orm/context.py | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 'lib/sqlalchemy') diff --git a/lib/sqlalchemy/orm/context.py b/lib/sqlalchemy/orm/context.py index 0868fb29b..d9e334d45 100644 --- a/lib/sqlalchemy/orm/context.py +++ b/lib/sqlalchemy/orm/context.py @@ -1170,7 +1170,18 @@ 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: + util.raise_( + sa_exc.ArgumentError( + "Join target %s does not refer to a " + "mapped entity" % right + ), + replace_context=err, + ) left = onclause._parententity @@ -1312,7 +1323,18 @@ 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: + util.raise_( + sa_exc.ArgumentError( + "Join target %s does not refer to a " + "mapped entity" % right + ), + replace_context=err, + ) left = onclause._parententity -- cgit v1.2.1