Reputation: 6483
I'm looking for a way to count all the records found by a query. I can see there is a count function but I'm not entirely sure how to deal with the output to get a number type out.
I have something similar to
entity::table_name::Entity::find().count(&db);
which returns a Pin<Box<dyn Future + Send<Output = Result<usize, DbErr>>>>
I'm just looking to get a number out. Am I on the right track here? What would be the simplest way to get the count?
Upvotes: 0
Views: 1400
Reputation: 6483
Getting the count as a usize is as simple as
entity::entity_name::Entity::find().count(&db).await.unwrap();
Where entity_name is the name of your table/entity.
Upvotes: 2
Reputation: 21
There is another way to do it:
#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
enum Counter {
Count,
}
entity::table_name::Entity::find()
.select_only()
.column_as(Expr::col(entity::table_name::Column::Id).count(), "count")
.into_values::<_, Counter>()
.one(&db)
.await
This looks however a bit complicated
Upvotes: 1