_memoized_property = util.group_expirable_memoized_property(["_columns"])
+ @util.pending_deprecation(
+ '1.1',
+ message="``Table.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]
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
class MetaDataTest(fixtures.TestBase, ComparesTables):
- def test_metadata_connect(self):
- metadata = MetaData()
- t1 = Table('table1', metadata,
- Column('col1', Integer, primary_key=True),
- Column('col2', String(20)))
- metadata.bind = testing.db
- metadata.create_all()
- try:
- assert t1.count().scalar() == 0
- finally:
- metadata.drop_all()
-
def test_metadata_contains(self):
metadata = MetaData()
t1 = Table('t1', metadata, Column('x', Integer))
data = os.urandom(32)
binary_table.insert().execute(data=data)
eq_(
- binary_table.select().where(binary_table.c.data == data).alias().
- count().scalar(), 1)
+ select([func.count('*')]).select_from(binary_table).
+ where(binary_table.c.data == data).scalar(), 1)
@testing.requires.binary_literals
def test_literal_roundtrip(self):