【发布时间】:2021-12-07 05:52:51
【问题描述】:
我正在使用柴油箱与 PostgreSQL 数据库进行通信。
我有一个用例,我想从表preferences 中获取数据到结构Preferences 中。
我希望数据库的输出由表中的两个字段过滤,在org_id 列中具有特定值,并在team_id 列中的值组中,我将它们的值存储在未知长度数组中,所以我不能做
sql_query(raw_sql).bind::<>().execute()
因为我不知道人数
bind::<>()
然后我尝试使用collect_binds
但它没有用,尝试执行以下操作
let mut filter = "".to_string();
let mut params:Vec<i64> = vec![];
let mut c =1;
let org_id = 10;
filter.push_str("(org_id=$1 AND team_id IN (");
params.push(org_id);
for team_id in get_needed_teams_id(){
c = c +1;
filter.push_str(&*format!("${}", c));
params.push(team_id );
}
filter.push_str("))");
let raw_sql = format!("SELECT * FROM preferences WHERE {} ORDER BY user_id ASC, team_id ASC", filter);
let preferences:Vec<Preferences> = vec![];
let sql_res = sql_query(raw_sql).collect_binds(&mut param, &preferences);
返回错误:
error[E0284]: type annotations needed: cannot satisfy `<_ as Backend>::BindCollector == Vec<i64>`
--> src\db\preference_db.rs:146:38
|
146 | let sql_res = sql_query(raw_sql).collect_binds(&mut params, &preferences);
| ^^^^^^^^^^^^^ cannot satisfy `<_ as Backend>::BindCollector == Vec<i64>`
【问题讨论】:
标签: postgresql rust rust-diesel