let count = users.count().get_result(&connection); assert_eq!(Ok(2), count);
get_result 返回一个 Result,在例子中是 Ok(2)。于是我写了这样一段代码:
1 2 3 4 5 6 7 8
let connection = establish_connection(); let uid_bindings_count = verifications.filter(uid.eq(self.uid)) .count() .get_result(&connection)?;
if uid_bindings_count < 2 { returnErr(OpError(NoMoreVerification)); }
但是编译的时候报错:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
error[E0277]: can't compare `()` with `i32` --> src\user\manager.rs:74:31 | 74 | if uid_bindings_count < 2 { | ^ no implementation for `() < i32` and `() > i32` | = help: the trait `std::cmp::PartialOrd<i32>` is not implemented for `()`
error[E0277]: the trait bound `(): diesel::Queryable<diesel::sql_types::BigInt, diesel::pg::Pg>` is not satisfied --> src\user\manager.rs:73:14 | 73 | .get_result(&connection)?; | ^^^^^^^^^^ the trait `diesel::Queryable<diesel::sql_types::BigInt, diesel::pg::Pg>` is not implemented for `()` | = note: required because of the requirements on the impl of `diesel::query_dsl::LoadQuery<diesel::PgConnection, ()>` for `diesel::query_builder::SelectStatement<schema::verifications::table, diesel::query_builder::select_clause::SelectClause<diesel::dsl::CountStar>, diesel::query_builder::distinct_clause::NoDistinctClause, diesel::query_builder::where_clause::WhereClause<diesel::expression::operators::Eq<schema::verifications::columns::uid, diesel::expression::bound::Bound<diesel::sql_types::Integer, i32>>>>`
// This will return `NotFound`, as there is no user with ID 4 let update_result = update(users.find(4)) .set(name.eq("Jim")) .get_result::<(i32, String)>(&connection); assert_eq!(Err(diesel::NotFound), update_result);
于是将我的代码改成
1 2 3
let uid_bindings_count = verifications.filter(uid.eq(self.uid)) .count() .get_result::<(i32)>(&connection)?;