From: Mike Bayer Date: Sat, 8 Nov 2008 21:18:11 +0000 (+0000) Subject: - Query.count() and Query.get() return a more informative X-Git-Tag: rel_0_5rc4~28 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e91bc867f5260abdfd23a95e76a056a7f11d9631;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - Query.count() and Query.get() return a more informative error message when executed against multiple entities. [ticket:1220] --- diff --git a/CHANGES b/CHANGES index 19f93fcae9..2b7095f285 100644 --- 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 e0c58611fc..c1c884879b 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.5.0rc3 +0.5.0rc4 diff --git a/lib/sqlalchemy/orm/query.py b/lib/sqlalchemy/orm/query.py index 51165287fb..39e3db43c6 100644 --- a/lib/sqlalchemy/orm/query.py +++ b/lib/sqlalchemy/orm/query.py @@ -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) diff --git a/test/orm/query.py b/test/orm/query.py index b63c12f092..c90707342c 100644 --- a/test/orm/query.py +++ b/test/orm/query.py @@ -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()