-
Hi, I'm little bit new in Rust and Sea query, I want to create an condition in loop, because I want to manipulate with some if statesment, and my code will be more convenient for reading use sea_query::{Iden, PostgresQueryBuilder, Query, Expr, LogicalChainOper, Condition, Cond};
use sea_query_binder::SqlxBinder;
use serde::{Deserialize, Serialize};
use sqlx::{Error, FromRow, PgPool};
#[derive(Iden)]
pub enum MyTable {
Table,
Id,
Version,
}
struct ValidatorParam {
id: String,
version: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
struct MyTableData {
pub id: String,
pub version: String,
}
pub async fn query_db (pool: PgPool) -> Result<Option<MyTableData>, Error> {
let mut select = Query::select()
.columns([
MyTable::Id,
MyTable::Version,
])
.from(MyTable::Table)
.to_owned();
let params = vec![
ValidatorParam{
id: "1".to_string(),
version: "1".to_string(),
},
ValidatorParam{
id: "2".to_string(),
version: "2".to_string(),
},
];
let mut where_condition = Condition::any();
for ValidatorParam { id, version } in ¶ms {
// here I got an error Value used after being moved
where_condition.add( // <---- here
Cond::all()
.add(Expr::col(MyTable::Id).eq(id))
.add(Expr::col(MyTable::Version).eq(id))
);
}
// select.cond_where(where_condition);
let (sql, values) = select
.build_sqlx(PostgresQueryBuilder);
sqlx::query_as_with::<_, MyTableData, _>(&sql, values)
.fetch_optional(&pool)
.await
} I'm little bit strugling with this, I could avoid this issue by using inside loop select.and_or_where(
LogicalChainOper::And(
... But I disslike, how my code, is looks. may be some bug inside Condtion, since, conditions vector is not explicitly declared as reference type. P.S. AI sux |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Hi. Can you paste the full error message? This would help to identify exactly:
If I were to guess, it looks like Perhaps, AI could guide you too, if you pasted your full function and the full error message into the prompt |
Beta Was this translation helpful? Give feedback.
-
// changes to make code working
let mut where_condition = Condition::any();
for ValidatorParam { id, version } in ¶ms {
where_condition = where_condition.add(
Cond::all()
.add(Expr::col(MyTable::Id).eq(id))
.add(Expr::col(MyTable::Version).eq(id))
);
}
select.cond_where(where_condition); |
Beta Was this translation helpful? Give feedback.
Hi. Can you paste the full error message? This would help to identify exactly:
If I were to guess, it looks like
where_condition.add(
consumes (moves) the condition (self
) and returns the updated condition back, rather than modifies the condition by reference (&mut self
). You need to assign the updated condition back to the variable:where_condition = where_condition.add(
. Your original code would be OK if that method accepted&mut self
.Perhaps, AI could guide you too, if you pasted your full function and the full error message into the prompt