MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / count

Method count

lib/sqlalchemy/orm/query.py:3147–3208  ·  view source on GitHub ↗

r"""Return a count of rows this the SQL formed by this :class:`Query` would return. This generates the SQL for this Query as follows: .. sourcecode:: sql SELECT count(1) AS count_1 FROM ( SELECT <rest of query follows...> ) AS anon_1

(self)

Source from the content-addressed store, hash-verified

3145 return sql.exists(inner)
3146
3147 def count(self) -> int:
3148 r"""Return a count of rows this the SQL formed by this :class:`Query`
3149 would return.
3150
3151 This generates the SQL for this Query as follows:
3152
3153 .. sourcecode:: sql
3154
3155 SELECT count(1) AS count_1 FROM (
3156 SELECT <rest of query follows...>
3157 ) AS anon_1
3158
3159 The above SQL returns a single row, which is the aggregate value
3160 of the count function; the :meth:`_query.Query.count`
3161 method then returns
3162 that single integer value.
3163
3164 .. warning::
3165
3166 It is important to note that the value returned by
3167 count() is **not the same as the number of ORM objects that this
3168 Query would return from a method such as the .all() method**.
3169 The :class:`_query.Query` object,
3170 when asked to return full entities,
3171 will **deduplicate entries based on primary key**, meaning if the
3172 same primary key value would appear in the results more than once,
3173 only one object of that primary key would be present. This does
3174 not apply to a query that is against individual columns.
3175
3176 .. seealso::
3177
3178 :ref:`faq_query_deduplicating`
3179
3180 For fine grained control over specific columns to count, to skip the
3181 usage of a subquery or otherwise control of the FROM clause, or to use
3182 other aggregate functions, use :attr:`~sqlalchemy.sql.expression.func`
3183 expressions in conjunction with :meth:`~.Session.query`, i.e.::
3184
3185 from sqlalchemy import func
3186
3187 # count User records, without
3188 # using a subquery.
3189 session.query(func.count(User.id))
3190
3191 # return count of user "id" grouped
3192 # by "name"
3193 session.query(func.count(User.id)).group_by(User.name)
3194
3195 from sqlalchemy import distinct
3196
3197 # count distinct "name" values
3198 session.query(func.count(distinct(User.name)))
3199
3200 .. seealso::
3201
3202 :ref:`migration_20_query_usage`
3203
3204 """

Callers 12

run_testMethod · 0.45
counterFunction · 0.45
check_winFunction · 0.45
check_loseFunction · 0.45
filter_public.pyFile · 0.45
nested_sets.pyFile · 0.45
test_external_idMethod · 0.45

Calls 3

_legacy_from_selfMethod · 0.95
enable_eagerloadsMethod · 0.80
scalarMethod · 0.45