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 Team, I am using debug_query macro which demands DB as generic parameter.
let sql = debug_query::<DB, _>(&users.count()).to_string();
What should be its value?
DB
in this scope
load<(ORM_Struct1, ORM_Struct2)>
, or .select((table_1_column1, table_1_column2, table_2_column1, table_2_column2 ...)).load(Combined_ORM_Struct1)
, But I cannot use .select((table_1_column1, table_1_column2, table_2_column1, table_2_column2 ...)).load(<ORM_Struct1, ORM_Struct2>)
, right?
Expression
as argument and each functions is a Expression
by itself. Just the types need to match, but that's always a requirement if you use diesel.
#[derive(Deserialize, Serialize)]
struct HttpRes<T> {
list: Vec<T>,
total: i64,
page: i64,
}
fn get_pagination_res<Table, T>(
pool: web::Data<Pool>,
table: Table,
page: i64,
) -> Result<HttpRes<T>, diesel::result::Error>
where
Table: diesel::associations::HasTable,
{
let conn = pool.get().unwrap();
let (list, total) = table.paginate(page).load_and_count_pages::<T>(&conn)?;
Ok(HttpRes { list, total, page })
}
Table: diesel::query_builder::Query
is not satisfieddiesel::query_builder::Query
for Paginated<Table>
LoadQuery<diesel::PgConnection, (T, i64)>
for Paginated<Table>
rustcE0277Paginated<Table>: QueryId
is not satisfiedLoadQuery<diesel::PgConnection, (T, i64)>
for Paginated<Table>
rustcE0277Table: diesel::query_builder::QueryFragment<Pg>
is not satisfieddiesel::query_builder::QueryFragment<Pg>
for Paginated<Table>
LoadQuery<diesel::PgConnection, (T, i64)>
for Paginated<Table>
rustcE0277T: diesel::Queryable<_, Pg>
is not satisfieddiesel::Queryable<(_, diesel::sql_types::BigInt), Pg>
for (T, i64)
LoadQuery<diesel::PgConnection, (T, i64)>
for Paginated<Table>
rustcE0277
FROM softprops/lambda-rust
RUN yum install -y mysql-devel
I want to write a function like composing applications with disel
to wrapper following query:
table_a::table.left_join(table_b::table)
.select(table_a::all_columns.nullable(), table_b::all_columns.nullable())
I can now define type using LeftJoin, but how could I define the return type for .select?
type BoxedQuery<'a> = BoxedSelectStatement<'a, (table_a::SqlType, table_b::SqlType), LeftJoin(table_a, table_b)>
, but it does't work
Select<LeftJoinAB, (a::SqlType, Nullable<b::SqlType>)>
, error shows10 error[E0277]: the trait bound `diesel::query_builder::SelectStatement<JoinOn<diesel::query_source::joins::Join<a::table, b::table, LeftOuter>, diesel::expression::operators::Eq<diesel::expression::nullable::Nullable<b::columns::a_id>, diesel::expression::nullable::Nullable<a::columns::id>>>>: SelectDsl<((diesel:\
:sql_types::Nullable<diesel::sql_types::Integer>, diesel::sql_types::Nullable<diesel::sql_types::Integer>), diesel::expression::nullable::Nullable<(diesel::sql_types::Nullable<diesel::sql_types::Integer>, diesel::sql_types::Nullable<diesel::sql_types::Integer>)>)>` is not satisfied
11 --> src/main.rs:22:1
12 |
13 22 | / fn query_ab() -> Select<LeftJoinAB, (a::SqlType, Nullable<b::SqlType>)> {
14 23 | | let join = a_left_join_b();
15 24 | | join.select((a::all_columns, b::all_columns.nullable()))
16 25 | | }
17 | |_^ the trait `SelectDsl<((diesel::sql_types::Nullable<diesel::sql_types::Integer>, diesel::sql_types::Nullable<diesel::sql_types::Integer>), diesel::expression::nullable::Nullable<(diesel::sql_types::Nullable<diesel::sql_types::Integer>, diesel::sql_types::Nullable<diesel::sql_types::Integer>)>)>` is not i\
mplemented for `diesel::query_builder::SelectStatement<JoinOn<diesel::query_source::joins::Join<a::table, b::table, LeftOuter>, diesel::expression::operators::Eq<diesel::expression::nullable::Nullable<b::columns::a_id>, diesel::expression::nullable::Nullable<a::columns::id>>>>`
Hello everyone, I am trying to implement optimistic locking generically over all structs which have some specific properties. Reason I want to do this, is because all of my structs are going to be optimistically locked, and I do not want to repeat the read-check-update code many times. The idea is that optimistic locking is basically just read and update. I have started writing the code, but generality of Diesel has left me awe-struck. Up to now I have following bit of code, and following error:
pub trait Versionable {
type Output: Identifiable;
fn update(&self, conn: &PgConnection) -> Self;
}
impl<T> Versionable for T where
T: Identifiable + HasTable
{
type Output = T;
fn update(&self, conn: &PgConnection) -> Self {
conn.transaction(|| {
let table = Self::table();
let pk = table.primary_key();
let row = table.filter(table, pk.eq(&self.id()));
unimplemented!()
});
unimplemented!()
}
}
and error resulting is
error[E0275]: overflow evaluating the requirement `_: Sized`
--> src/product/category/models.rs:62:29
|
62 | let row = table.filter(table, pk.eq(&self.id()));
| ^^^^^^
|
= help: consider adding a `#![recursion_limit="4096"]` attribute to your crate (`warehouse`)
= note: required because of the requirements on the impl of `FilterDsl<_>` for `<<T as HasTable>::Table as diesel::query_builder::AsQuery>::Query`
It seems like the content of <<T as HasTable>::Table as diesel::query_builder::AsQuery>::Query
may again be a type implementing trait Table
, which leads to infinite recursion. The path by which this seems to be possible is QueryDsl::filter -> FilterDsl<Predicate>::filter -> QueryDsl::filter ....
.
My first question is how do I resolve this? How do I say that <<T as HasTable>::Table as diesel::query_builder::AsQuery>::Query
is actually something which does not loop back to QueryDsl
via FilteringDsl<Predicate>
?
Second question I have is that I have noticed, that I have no constraint that primary key, here variable pk
is comparable to &self.id()
. How would one express that using Diesel traits?