From 592300728ce0be241dde3f447750a93779917646 Mon Sep 17 00:00:00 2001 From: RamonWill Date: Sun, 12 Jul 2020 21:34:09 +0100 Subject: [PATCH] resolves #4428 --- doc/build/changelog/unreleased_14/4428.rst | 8 ++++++++ lib/sqlalchemy/orm/context.py | 10 +++++++++- test/orm/test_joins.py | 13 +++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 doc/build/changelog/unreleased_14/4428.rst diff --git a/doc/build/changelog/unreleased_14/4428.rst b/doc/build/changelog/unreleased_14/4428.rst new file mode 100644 index 0000000000..aaef0c9f02 --- /dev/null +++ b/doc/build/changelog/unreleased_14/4428.rst @@ -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. diff --git a/lib/sqlalchemy/orm/context.py b/lib/sqlalchemy/orm/context.py index 55a6b4cd21..564845e7de 100644 --- a/lib/sqlalchemy/orm/context.py +++ b/lib/sqlalchemy/orm/context.py @@ -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 diff --git a/test/orm/test_joins.py b/test/orm/test_joins.py index 4ffa5fb9e9..9b573ec1e9 100644 --- a/test/orm/test_joins.py +++ b/test/orm/test_joins.py @@ -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().""" -- 2.47.3