diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2016-06-13 15:18:13 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2016-06-14 11:48:04 -0400 |
| commit | f38f890849700ee1bf719a31275260e2da455bc3 (patch) | |
| tree | e5d8a958d853f05081e57045baa8c9806ad8f27a /lib/sqlalchemy/sql | |
| parent | 7189d0bc82598c2d6dcbb55b054837416db2ee7d (diff) | |
| download | sqlalchemy-f38f890849700ee1bf719a31275260e2da455bc3.tar.gz | |
Deprecate FromClause.count()
count() here is misleading in that it not only
counts from an arbitrary column in the table, it also
does not make accommodations for DISTINCT, JOIN, etc.
as the ORM-level function does. Core should not be
attempting to provide a function like this.
Change-Id: I9916fc51ef744389a92c54660ab08e9695b8afc2
Fixes: #3724
Diffstat (limited to 'lib/sqlalchemy/sql')
| -rw-r--r-- | lib/sqlalchemy/sql/selectable.py | 41 |
1 files changed, 25 insertions, 16 deletions
diff --git a/lib/sqlalchemy/sql/selectable.py b/lib/sqlalchemy/sql/selectable.py index f75613e35..ac955a60f 100644 --- a/lib/sqlalchemy/sql/selectable.py +++ b/lib/sqlalchemy/sql/selectable.py @@ -308,10 +308,34 @@ class FromClause(Selectable): _memoized_property = util.group_expirable_memoized_property(["_columns"]) + @util.deprecated( + '1.1', + message="``FromClause.count()`` is deprecated. Counting " + "rows requires that the correct column expression and " + "accommodations for joins, DISTINCT, etc. must be made, " + "otherwise results may not be what's expected. " + "Please use an appropriate ``func.count()`` expression " + "directly.") @util.dependencies("sqlalchemy.sql.functions") def count(self, functions, whereclause=None, **params): """return a SELECT COUNT generated against this - :class:`.FromClause`.""" + :class:`.FromClause`. + + The function generates COUNT against the + first column in the primary key of the table, or against + the first column in the table overall. Explicit use of + ``func.count()`` should be preferred:: + + row_count = conn.scalar( + select([func.count('*')]).select_from(table) + ) + + + .. seealso:: + + :data:`.func` + + """ if self.primary_key: col = list(self.primary_key)[0] @@ -1610,21 +1634,6 @@ class TableClause(Immutable, FromClause): else: return [] - @util.dependencies("sqlalchemy.sql.functions") - def count(self, functions, whereclause=None, **params): - """return a SELECT COUNT generated against this - :class:`.TableClause`.""" - - if self.primary_key: - col = list(self.primary_key)[0] - else: - col = list(self.columns)[0] - return Select( - [functions.func.count(col).label('tbl_row_count')], - whereclause, - from_obj=[self], - **params) - @util.dependencies("sqlalchemy.sql.dml") def insert(self, dml, values=None, inline=False, **kwargs): """Generate an :func:`.insert` construct against this |
