summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2008-11-08 21:18:11 +0000
committerMike Bayer <mike_mp@zzzcomputing.com>2008-11-08 21:18:11 +0000
commite91bc867f5260abdfd23a95e76a056a7f11d9631 (patch)
tree9540b683448189bac6f534fef1aa88b62e56cf57
parent1901519fa7777ad84ed12827c192e57b7ef92139 (diff)
downloadsqlalchemy-e91bc867f5260abdfd23a95e76a056a7f11d9631.tar.gz
- Query.count() and Query.get() return a more informative
error message when executed against multiple entities. [ticket:1220]
-rw-r--r--CHANGES6
-rw-r--r--VERSION2
-rw-r--r--lib/sqlalchemy/orm/query.py17
-rw-r--r--test/orm/query.py8
4 files changed, 26 insertions, 7 deletions
diff --git a/CHANGES b/CHANGES
index 19f93fcae..2b7095f28 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 e0c58611f..c1c884879 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 51165287f..39e3db43c 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 b63c12f09..c90707342 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()