From: Michael Williamson Date: Sun, 28 Aug 2016 11:38:04 +0000 (+0100) Subject: Provide more informative error when joining with no entities X-Git-Tag: rel_1_1_0~42^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=94fe6fe05dacf6f38dd0541fd59b0ca76b440c4e;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Provide more informative error when joining with no entities --- diff --git a/lib/sqlalchemy/orm/query.py b/lib/sqlalchemy/orm/query.py index f15f4340b1..592c685ca0 100644 --- a/lib/sqlalchemy/orm/query.py +++ b/lib/sqlalchemy/orm/query.py @@ -2125,10 +2125,15 @@ class Query(object): left = self._entities[0].entity_zero_or_selectable if left is None: + if self._entities: + problem = "Don't know how to join from %s" % self._entities[0] + else: + problem = "No entities to join from" + raise sa_exc.InvalidRequestError( - "Don't know how to join from %s; please use " + "%s; please use " "select_from() to establish the left " - "entity/selectable of this join" % self._entities[0]) + "entity/selectable of this join" % problem) if left is right and \ not create_aliases: diff --git a/test/orm/test_joins.py b/test/orm/test_joins.py index e14af635d7..01f8627bc9 100644 --- a/test/orm/test_joins.py +++ b/test/orm/test_joins.py @@ -447,6 +447,19 @@ class JoinTest(QueryTest, AssertsCompiledSQL): sess.query(literal_column('x'), User).join, Address ) + def test_left_is_none_and_query_has_no_entities(self): + User = self.classes.User + Address = self.classes.Address + + sess = create_session() + + assert_raises_message( + sa_exc.InvalidRequestError, + "No entities to join from; please use select_from\(\) to " + "establish the left entity/selectable of this join", + sess.query().join, Address + ) + def test_isouter_flag(self): User = self.classes.User