diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2009-07-25 18:54:20 +0000 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2009-07-25 18:54:20 +0000 |
| commit | 066bdaec7569fa3bda2f0fb9fd540af2a685ace0 (patch) | |
| tree | 02bb18b30a0b1c050c4222d862ccccae2d67c408 /lib | |
| parent | ed8742e6858f11d48c78fcbbad35e92834aa47f0 (diff) | |
| download | sqlalchemy-066bdaec7569fa3bda2f0fb9fd540af2a685ace0.tar.gz | |
beefed up documentation for count(), [ticket:1465]
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/sqlalchemy/orm/query.py | 30 |
1 files changed, 18 insertions, 12 deletions
diff --git a/lib/sqlalchemy/orm/query.py b/lib/sqlalchemy/orm/query.py index c0eb3b02a..043ee1568 100644 --- a/lib/sqlalchemy/orm/query.py +++ b/lib/sqlalchemy/orm/query.py @@ -1450,18 +1450,24 @@ class Query(object): kwargs.get('distinct', False)) def count(self): - """Apply this query's criterion to a SELECT COUNT statement. - - If column expressions or LIMIT/OFFSET/DISTINCT are present, - the query "SELECT count(1) FROM (SELECT ...)" is issued, - so that the result matches the total number of rows - this query would return. For mapped entities, - the primary key columns of each is written to the - columns clause of the nested SELECT statement. - - For a Query which is only against mapped entities, - a simpler "SELECT count(1) FROM table1, table2, ... - WHERE criterion" is issued. + """Return a count of rows this Query would return. + + For simple entity queries, count() issues + a SELECT COUNT, and will specifically count the primary + key column of the first entity only. If the query uses + LIMIT, OFFSET, or DISTINCT, count() will wrap the statement + generated by this Query in a subquery, from which a SELECT COUNT + is issued, so that the contract of "how many rows + would be returned?" is honored. + + For queries that request specific columns or expressions, + count() again makes no assumptions about those expressions + and will wrap everything in a subquery. Therefore, + ``Query.count()`` is usually not what you want in this case. + To count specific columns, often in conjunction with + GROUP BY, use ``func.count()`` as an individual column expression + instead of ``Query.count()``. See the ORM tutorial + for an example. """ should_nest = [self._should_nest_selectable] |
