A safe, extensible ORM and Query Builder for Rust – For feature requests and longer questions also see https://github.com/diesel-rs/diesel/discussions/categories/q-a
sql_query
requires always a custom struct that implements QueryableByName
. Instead of using sql_query
you could define your version function via sql_function!
and use the typed query builder (so diesel::select(version())
which allows you to select directly into a String
.sql_query
.
QueryableByName
. That seems so be what's in the example, at least.
the trait bound `query_builder::sql_query::UncheckedBind<SqlQuery, u32, diesel::sql_types::Unsigned<diesel::sql_types::Integer>>: LoadQuery<_, Exhibitions>` is not satisfied
the following implementations were found:
<query_builder::sql_query::UncheckedBind<Query, Value, ST> as LoadQuery<Conn, T>>
#[derive(QueryableByName)]
struct Exhibitions {
#[sql_type = "String"]
exhibitions: String,
}
#[post("/sqlconnect/readExhibitions.php", data = "<form>")]
pub fn get_handler(
form: Form<GetHandlerForm>,
conn: Database,
) -> Result<JsonValue, Custom<String>> {
Ok(
match sql_query(include_str!("select_exhibitions.sql"))
.bind::<Unsigned<Integer>, _>(form.cid)
.load::<Exhibitions>(&*conn)
.map_err(|e| Custom(Status::InternalServerError, e.to_string()))?
.pop()
.map(|w| w.exhibitions)
{
Some(json) => serde_json::to_value(json)
.map_err(|e| {
Custom(
Status::InternalServerError,
format!("Failed to serialize Exhibitions: {}", e.to_string()),
)
})?
.into(),
None => json!({
"exhibitions": []
}),
},
)
}
It's at the call to load
. Since Exhibitions
derives from QueryableByName
, I thought I could load it from the query, but something is wrong.
exhibitions
member of the struct refers to the exhibitions
column in the table returned by my custom query. And that's a big JSON_OBJECT
.
Is it possible to conditionally perform update on conflicts with multiple values? (I'm using postrgres)
Something like this:
#[derive(Insertable)]
struct InsertPost {
id: i64,
date: NaiveDate,
}
let insert_vec = vec![Test{id: 1, date: "2014.05.05"}, Test{id: 2, date: "2013.03.05"}];
diesel::insert_into(tests)
.values(&insert_vec)
.on_conflict(id)
.do_update()
.filter(date.ge(excluded(date)))
.set(date.eq(excluded(date)));
I want to insert multiple values and only update the rows that meet a certain criteria, is there any way to acheive this?
The example above does not compile as the type IncompleteDoUpdate
type does not contain a filter method.