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
hi, I'm trying to construct this where clause with nested and/or:
SELECT *
FROM `pieces_users`
WHERE `user_id` = ?
AND (
(`itemType` = ? AND `bookId` > ?)
OR
(`itemType` = ? AND `bundleId` > ?)
);
Here's what I tried:
let query = pieces_users
.filter(
pu_user_id.eq(user_id_).and(
(pu_item_type.eq(1).and(pu_book_id.gt(0)))
.or(pu_item_type.eq(0).and(pu_bundle_id.gt(0))),
),
)
.select(pieces_users::all_columns())
However the generated SQL WHERE is flattened:
SELECT *
FROM `pieces_users`
WHERE `user_id` = ?
AND (`itemType` = ? AND `bookId` > ? OR `itemType` = ? AND `bundleId` > ?)
How can I control the nested AND/OR construction? I've read this https://docs.diesel.rs/master/diesel/expression_methods/trait.BoolExpressionMethods.html#method.and but it only contains simpler cases
This is what I currently have:
fn load<M>(&self, id: M::Id, connection: &PgConnection) ->QueryResult<M>
where
M: Identifiable,
Find<dyn FindDsl<M::Id, Output = M>, M::Id>: LoadQuery<PgConnection, M>,
{
M::table().find(id).get_result(connection)
It doesn't compile:
error[E0275]: overflow evaluating the requirement `_: Sized`
--> src/bin/server/database.rs:179:16
|
179 | I::table().find(id).get_result(conn)
| ^^^^
|
= help: consider adding a `#![recursion_limit="4096"]` attribute to your crate (`server`)
= note: required because of the requirements on the impl of `FilterDsl<_>` for `<<I as HasTable>::Table as AsQuery>::Query`
error: aborting due to previous error; 6 warnings emitted
534.12
into a pgnumeric, but it doesn't work
Hello everyone. I got a question on raw queries. Haven’t found much on diesel’s documentation.
const QUERY: &str = r#"SELECT "id", "user_id", "name", "location", ST_Distance(...) as "distance" FROM profile WHERE ST_Distance(...) <= 1000 ORDER BY "distance" ASC"#;
let profiles = diesel::sql_query(QUERY).load::<ProfileNearby>(_connection);
I’m wondering how can I use my custom structs with diesel and raw queries. If I use load::<Profile> everything is ok and it builds fine, the only problem is that result doesn’t have distance column as it is not specified on a struct and doesn’t exists on table. Do you know how I can bind a custom struct to query results ?
eq
methods seems only to work on boolean ? How can I check if an integer is equal to another integer ?
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
.