]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Provide more informative error when joining with no entities
authorMichael Williamson <mike@zwobble.org>
Sun, 28 Aug 2016 11:38:04 +0000 (12:38 +0100)
committerMichael Williamson <mike@zwobble.org>
Sun, 28 Aug 2016 11:38:04 +0000 (12:38 +0100)
lib/sqlalchemy/orm/query.py
test/orm/test_joins.py

index f15f4340b187153d499cd646194ff8d77ede88ad..592c685ca0e10c2dea539ccdfab70cb7bbd85184 100644 (file)
@@ -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:
index e14af635d72fe0f998e929879b60c5801133d57e..01f8627bc9ff9882e1f558f7d0bc4184d6ae134b 100644 (file)
@@ -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