- Query.count() and Query.get() return a more informative

error message when executed against multiple entities.
[ticket:1220]
This commit is contained in:
Mike Bayer
2008-11-08 21:18:11 +00:00
parent 1901519fa7
commit e91bc867f5
4 changed files with 26 additions and 7 deletions
+6
View File
@@ -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.
+1 -1
View File
@@ -1 +1 @@
0.5.0rc3
0.5.0rc4
+11 -6
View 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)
+8
View 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()