I eventually figured this out, but through a long torturous process. What is the correct way to figure this out?
This line is almost correct, it uses diesel to return count of all records.
users.select(count_star()).first(&*conn).unwrap()
This is the error
error[E0282]: type annotations needed
--> src/mylib.rs:20:29
|
20 | users.select(count_star()).first(&*conn).unwrap();
| ^^^^^ cannot infer type for `U`
Ok cool, I go to the documentation for first()
https://docs.diesel.rs/diesel/query_dsl/trait.RunQueryDsl.html#method.first
function sig pasted below
fn first<U>(self, conn: &Conn) -> QueryResult<U>
where
Self: LimitDsl,
Limit<Self>: LoadQuery<Conn, U>,
Ok good, but what are my options for U? Where do I go from here? I figure a "count" will be an integer so I try i32
users.select(count_star()).first::<i32>(&*conn).unwrap();
I get the error
error[E0277]: the trait bound `i32: diesel::deserialize::FromSql<diesel::sql_types::BigInt, _>` is not satisfied
--> src/mylib.rs:20:29
|
20 | users.select(count_star()).first::<i32>(&*conn).unwrap();
| ^^^^^ the trait `diesel::deserialize::FromSql<diesel::sql_types::BigInt, _>` is not implemented for `i32`
|
= help: the following implementations were found:
<i32 as diesel::deserialize::FromSql<diesel::sql_types::Integer, DB>>
= note: required because of the requirements on the impl of `diesel::Queryable<diesel::sql_types::BigInt, _>`
for `i32`
= note: required because of the requirements on the impl of `diesel::query_dsl::LoadQuery<_, i32>` for `diesel::query_builder::SelectStatement<schema::users::table, diesel::query_builder::select_clause::SelectClause<diesel::dsl::CountStar>, diesel::query_builder::distinct_clause::NoDistinctClause, diesel::query_builder::where_clause::NoWhereClause, diesel::query_builder::order_clause::NoOrderClause, diesel::query_builder::limit_clause::LimitClause<diesel::expression::bound::Bound<diesel::sql_types::BigInt, i64>>>`
Ok, so I look at the part that says
the trait `diesel::deserialize::FromSql<diesel::sql_types::BigInt, _>` is not implemented for `i32`
I look it up here
https://docs.diesel.rs/diesel/deserialize/trait.FromSql.html
I see the only match for i32 is for sqlite but I'm using mysql, i64 is what I can use!
impl<DB: Backend<RawValue = [u8]>> FromSql<BigInt, DB> for i64
Finally I've arrived:
users.select(count_star()).first::<i64>(&*conn).unwrap()
What I'm trying to convey is that only through compiler errors could I get anywhere. Is this how its done? Did I miss a step in the docs somewhere?