尝试从表中获取总计数时, rust 柴油面临错误

标签 rust rust-diesel

我正在使用 Rust 实现数据库 diesel 。我想使用 filter 查询来获取表中存在的总计数或总列数健康)状况。下面是我的表结构和查询代码。我没有使用任何BIGINT, 我的表结构中的大十进制。

the trait bound i32: FromSql<BigInt, Pg> is not satisfied the following implementations were found: <i32 as FromSql<Integer, DB>> required because of the requirements on the impl of diesel::Queryable<BigInt, Pg> for i32 required because of the requirements on the impl of LoadQuery<PooledConnection<ConnectionManager<PgConnection>>, i32> for

#Emplyee table

| employee-id  | employee_name | empolyee_email|       
| -----------  | --------------|-------------  |
| 1            | ABC           |<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="badbd8d9fad7dbd3d694d9d5d7" rel="noreferrer noopener nofollow">[email protected]</a>   |
| 2            | xyz           |<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5c2425261c313d3530723f3331" rel="noreferrer noopener nofollow">[email protected]</a>   |


# Account table

| account  | employee-id    | account-balnce | created_at|
| -------- | ----------     |--------------- |-----------|
| 1        | 1              |   2000         | 22/10/2021|
| 2        | 2              |   5000         | 01/09/2021|

fn get_total_accounts(&self, employee_id: &str) -> anyhow::Result<Option<i32>> {
        let res: i32 = employee::table
            .inner_join(account::table)
            .filter(employee::dsl::employee_id.eq(employee_id))
            .count()
            .get_result(&self.pool.get()?)?;  //get_result through error
               
 }

最佳答案

使用 I64 解决了我的问题。

fn get_total_accounts(&self, employee_id: &str) -> anyhow::Result<Option<i32>> {
        let res: i64 = employee::table
            .inner_join(account::table)
            .filter(employee::dsl::employee_id.eq(employee_id))
            .count()
            .get_result(&self.pool.get()?)?;  //get_result through error
               
 }

关于尝试从表中获取总计数时, rust 柴油面临错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70182006/

相关文章:

arrays - 在当前作用域中未找到针对struct `contains`的名为 `schema::users::columns::roles`的方法

rust - 自动解除引用和解除引用强制之间有什么关系?

multithreading - 你能为线程指定一个非静态的生命周期吗?

rust - 如何使用宏将元组扩展为其成员作为函数参数?

rust 柴油有条件地过滤一个查询

rust - 实现基类型(float)的特征

rust - 如何将哈希转换为字符串? [复制]

rust - 我如何使用Diesel的主键以外的其他列查找值?

heroku - 将 Rust 应用程序部署到 Heroku 时如何修复 "cannot find -lsqlite3"错误?