]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Query.count() and Query.get() return a more informative
authorMike Bayer <mike_mp@zzzcomputing.com>
Sat, 8 Nov 2008 21:18:11 +0000 (21:18 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sat, 8 Nov 2008 21:18:11 +0000 (21:18 +0000)
error message when executed against multiple entities.
[ticket:1220]

CHANGES
VERSION
lib/sqlalchemy/orm/query.py
test/orm/query.py

diff --git a/CHANGES b/CHANGES
index 19f93fcae96d1c51783874b1e9c0793fd8a2652b..2b7095f285c349e92a29b246bde6dc6e640ffeef 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -6,6 +6,12 @@ CHANGES
 =======
 0.5.0rc4
 ========
+- bugfixes and behavioral changes
+- orm
+    - Query.count() and Query.get() return a more informative
+      error message when executed against multiple entities.
+      [ticket:1220]
+
 - mssql
     - Lots of cleanup and fixes to correct problems with
       limit and offset.
diff --git a/VERSION b/VERSION
index e0c58611fcca95780c89e1ba01f65f36e42d7fa0..c1c884879b08779f9da239e855edcdd611e15600 100644 (file)
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-0.5.0rc3
+0.5.0rc4
index 51165287fb181b62519389b79eea336f51fcdd29..39e3db43c6336050ced196e3e6862d795146283f 100644 (file)
@@ -236,14 +236,14 @@ class Query(object):
             return None
         return self._entities[0].mapper
 
-    def _only_mapper_zero(self):
+    def _only_mapper_zero(self, rationale=None):
         if len(self._entities) > 1:
-            raise sa_exc.InvalidRequestError("This operation requires a Query against a single mapper.")
+            raise sa_exc.InvalidRequestError(rationale or "This operation requires a Query against a single mapper.")
         return self._mapper_zero()
 
-    def _only_entity_zero(self):
+    def _only_entity_zero(self, rationale=None):
         if len(self._entities) > 1:
-            raise sa_exc.InvalidRequestError("This operation requires a Query against a single mapper.")
+            raise sa_exc.InvalidRequestError(rationale or "This operation requires a Query against a single mapper.")
         return self._entity_zero()
 
     def _generate_mapper_zero(self):
@@ -410,7 +410,7 @@ class Query(object):
         if hasattr(ident, '__composite_values__'):
             ident = ident.__composite_values__()
 
-        key = self._only_mapper_zero().identity_key_from_primary_key(ident)
+        key = self._only_mapper_zero("get() can only be used against a single mapped class.").identity_key_from_primary_key(ident)
         return self._get(key, ident)
 
     @classmethod
@@ -1248,7 +1248,12 @@ class Query(object):
     def count(self):
         """Apply this query's criterion to a SELECT COUNT statement."""
 
-        return self._col_aggregate(sql.literal_column('1'), sql.func.count, nested_cols=list(self._only_mapper_zero().primary_key))
+        return self._col_aggregate(sql.literal_column('1'), sql.func.count, 
+            nested_cols=list(self._only_mapper_zero(
+                "Can't issue count() for multiple types of objects or columns. "
+                " Construct the Query against a single element as the thing to be counted, "
+                "or for an actual row count use Query(func.count(somecolumn)) or "
+                "query.values(func.count(somecolumn)) instead.").primary_key))
 
     def _col_aggregate(self, col, func, nested_cols=None):
         context = QueryContext(self)
index b63c12f09216295c91d42c45edbb0f0c49afbf19..c90707342cf5605cf001f7570b4744f8676f4a46 100644 (file)
@@ -236,6 +236,14 @@ class InvalidGenerationsTest(QueryTest):
         # this is fine, however
         q.from_self()
     
+    def test_mapper_zero(self):
+        s = create_session()
+        
+        q = s.query(User, Address)
+        self.assertRaises(sa_exc.InvalidRequestError, q.count)
+
+        self.assertRaises(sa_exc.InvalidRequestError, q.get, 5)
+        
     def test_from_statement(self):
         s = create_session()