我正在使用 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 ofdiesel::Queryable<BigInt, Pg>
fori32
required because of the requirements on the impl ofLoadQuery<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/