Replies: 2 comments 3 replies
-
Would it be possible to provide the complete error message as emitted by rustc and not a truncated version? That makes it much easier for us to see what's wrong here. Also you might want to include more information in your source code as well. Ideally that should be a self contained minimal example. At least it should contain definitions and trait implementations for all types not from diesel. |
Beta Was this translation helpful? Give feedback.
0 replies
-
According to your suggestion, I wrote a "simple" example displaying this behaviour. Minimum reproductionuse std::error::Error;
use diesel::{
pg::Pg,
query_builder::{Query, QueryFragment, QueryId},
query_dsl::methods::{LoadQuery, SelectDsl},
sql_types::BigInt,
Connection, PgConnection, QueryResult, Queryable, RunQueryDsl, Selectable, SelectableHelper,
};
pub trait Paginate: Sized {
fn paginate(self) -> Pagination<Self>;
}
impl<T> Paginate for T {
fn paginate(self) -> Pagination<Self> {
Pagination { query: self }
}
}
#[derive(Debug, Clone, Copy, QueryId)]
pub struct Pagination<T> {
query: T,
}
impl<T> QueryFragment<Pg> for Pagination<T>
where
T: QueryFragment<Pg>,
{
fn walk_ast<'b>(
&'b self,
mut out: diesel::query_builder::AstPass<'_, 'b, Pg>,
) -> QueryResult<()> {
out.push_sql("SELECT *, COUNT(*) OVER () FROM(");
self.query.walk_ast(out.reborrow())?;
out.push_sql(")");
Ok(())
}
}
impl<T: Query> Query for Pagination<T> {
type SqlType = (T::SqlType, BigInt);
}
impl<T> diesel::RunQueryDsl<PgConnection> for Pagination<T> {}
// this is the function not working
impl<T> Pagination<T> {
pub fn load_with_info<'a, U>(self, conn: &mut PgConnection) -> QueryResult<(Vec<U>, i64)>
where
Self: LoadQuery<'a, PgConnection, (U, i64)>,
{
let results = self.load::<(U, i64)>(conn)?;
let total = results.get(0).map(|x| x.1).unwrap_or(0);
let records = results.into_iter().map(|x| x.0).collect();
Ok((records, total))
}
}
// define a simple table
diesel::table! {
my_table (field_a) {
field_a -> Int4,
field_b -> Int4,
}
}
// define 2 query structs on this table
#[derive(Queryable, Selectable)]
#[diesel(table_name = my_table)]
#[diesel(check_for_backend(diesel::pg::Pg))]
struct PartA {
field_a: i32,
}
#[derive(Queryable, Selectable)]
#[diesel(table_name = my_table)]
#[diesel(check_for_backend(diesel::pg::Pg))]
struct PartB {
field_b: i32,
}
fn main() -> Result<(), Box<dyn Error>> {
let mut conn = PgConnection::establish("").expect("Error connecting to database");
// this works
let result: Vec<(_, _)> = my_table::table
.select((PartA::as_select(), PartB::as_select()))
.load(&mut conn)?;
// this does not compile
let (result, size): (Vec<(_, _)>, i64) = my_table::table
.select((PartA::as_select(), PartB::as_select()))
.paginate()
.load_with_info(&mut conn)?;
Ok(())
} Here is the error produced by the compiler: cargo error message
|
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello !
I have a question (and a problem) about implementing a custom QueryFragment. I followed the steps in the guide, a wrote my query fragment with a custom
load_with_infos
method:(don’t mind the async, I’m using diesel_async)
This implementation is working fine for most cases, but I have a special use case somewhere.
I have to structs querying different information on the same table, like this:
My original intent was to query both structs at once with a tuple (this works with regular load):
However, I get an error from the compiler:
Any idea where this might come from ?
Beta Was this translation helpful? Give feedback.
All reactions