trait not satisfied for select(count_star()).first

486 views Asked by At

I'd like to do a count on records of a table named topics. I'm doing it almost by the book

but when I do

let count: u64 = topics.select(count_star()).first(&conn).unwrap();

I keep getting (.first is underlined as error):

[rustc E0277] [E] the trait bound `u64: diesel::deserialize::FromSql<diesel::
sql_types::BigInt, diesel::mysql::Mysql>` is not satisfied

the trait `diesel::deserialize::FromSql<diesel::sql_types::BigInt, diesel::
mysql::Mysql>` is not implemented for `u64`

help: the following implementations were found:
    <u64 as diesel::deserialize::FromSql<diesel::sql_types::
// I guess there's more information but Vim's
// Pmenu only shows up to eight lines.

So propably I need to do some type casting here, but honestly, I have no idea at which point.

1

There are 1 answers

0
Masklinn On BEST ANSWER

As the error tells you, count_star generates a BigInt.

And as BigInt's documentation indicates, BigInt is convertible to and from i64, not u64.

I guess despite MySQL having unsigned integers Diesel doesn't support them because SQL doesn't specify unsigned integers, so not all database engines have them (e.g. Postgres does not).

Anyway your count should be an i64, or you need to set up a more explicit conversion from a BigInt to an i64 to an u64.