我正在柴油中发布一个原始sql。如果没有参数,代码就会编译。但是,当我使用bind向sql_query添加参数时,代码将不会编译。
pub fn find(session_id: Uuid) -> Result<Vec<Summary>, CustomError> {
let q = "select product, sum(price) as price from items where session_id = $1 group by product order by product";
let mut conn = db::connection()?;
let item = diesel::sql_query(q)
//.bind::<Uuid, _>(session_id) <-- won't compile
.get_results(&mut conn)?;
Ok(item)
}当我添加行.bind::<Uuid, _>(session_id)货物生成失败时
error[E0277]: the trait bound `uuid::Uuid: ToSql<uuid::Uuid, _>` is not satisfied
--> src/items/models.rs:52:14
|
52 | .get_results(&mut conn)?;
| ^^^^^^^^^^^ the trait `ToSql<uuid::Uuid, _>` is not implemented for `uuid::Uuid`
|
= help: the following other types implement trait `ToSql<A, DB>`:
<uuid::Uuid as ToSql<Nullable<diesel::sql_types::Uuid>, __DB>>
<uuid::Uuid as ToSql<diesel::sql_types::Uuid, Pg>>
= note: required because of the requirements on the impl of `QueryFragment<_>` for `query_builder::sql_query::UncheckedBind<SqlQuery, uuid::Uuid, uuid::Uuid>`
= note: required because of the requirements on the impl of `LoadQuery<'_, _, _>` for `query_builder::sql_query::UncheckedBind<SqlQuery, uuid::Uuid, uuid::Uuid>`
note: required by a bound in `get_results`
--> /Users/claus/.cargo/registry/src/github.com-1ecc6299db9ec823/diesel-2.0.2/src/query_dsl/mod.rs:1695:15
|
1695 | Self: LoadQuery<'query, Conn, U>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `get_results`Cargo.toml:
actix-web = "4.2.1"
actix-rt = "2.7.0"
diesel = { version = "2.0.2", features = ["postgres", "r2d2", "uuid", "chrono", "numeric", "serde_json"] }
serde = { version = "1.0.148", features = ["derive"] }
uuid = { version = "1.2.2", features = ["serde", "v4"] }
bigdecimal = { version = "0.3.0", features = ["serde"] }带有一些绑定示例的柴油车。
发布于 2022-11-29 23:12:50
发布后,我尝试更改绑定到diesel::sql_types::Uuid中的Uuid类型。现在代码编译了。所以,如果其他人有类似的问题,就发布一个答案。
.bind::<diesel::sql_types::Uuid, _>(session_id)https://stackoverflow.com/questions/74621239
复制相似问题