Operations (e.g. multiply) on columns from a join in select_expression
#4670
-
I've only found #3970 regarding multiplication, but this seems to be a different case. For reference, I'm running Diesel 2.2.6. diesel::table! {
species_substance_safety (substance_id) {
maximum_milligrams_per_kilo_per_day -> Float8,
milligrams_per_kilo_per_day -> Float8,
substance_id -> Uuid,
}
}
diesel::table! {
medicine_ingredients (substance_id) {
dosage_per_unit -> Float8,
substance_id -> Uuid,
}
} I want to run a join with this (also abbreviated) struct as a result (the use crate::schema::{medicine_ingredients, species_substance_safety};
#[derive(Queryable, Selectable)]
#[diesel(check_for_backend(diesel::pg::Pg))]
pub struct SubstanceSafetyForSpecies {
#[diesel(select_expression =
species_substance_safety::maximum_milligrams_per_kilo_per_day *
medicine_ingredients::dosage_per_unit
)]
pub maximum_dose_per_kilo_per_day: f64,
#[diesel(select_expression =
species_substance_safety::milligrams_per_kilo_per_day *
medicine_ingredients::dosage_per_unit
)]
pub recommended_dose_per_kilo_per_day: f64,
} I'm running this join: species_substance_safety::table
.inner_join(substances::table.inner_join(medicine_ingredients::table))
.filter(species_substance_safety::substance_id.eq_any(substance_ids))
.select(Self::as_select())
.load::<Self>(conn) I have a hunch that a function with I can select the results into a tuple, but for the life of me I can't get the |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Can you provide the error message that you get? I cannot even reproduce your problem as the following code compiles without error for me: mod schema {
diesel::table! {
species_substance_safety (substance_id) {
maximum_milligrams_per_kilo_per_day -> Float8,
milligrams_per_kilo_per_day -> Float8,
substance_id -> Uuid,
}
}
diesel::table! {
medicine_ingredients (substance_id) {
dosage_per_unit -> Float8,
substance_id -> Uuid,
}
}
diesel::table! {
substances {
id -> Uuid,
}
}
diesel::allow_tables_to_appear_in_same_query!(
species_substance_safety,
medicine_ingredients,
substances
);
diesel::joinable!(medicine_ingredients -> substances (substance_id));
diesel::joinable!(species_substance_safety -> substances (substance_id));
}
use crate::schema::{medicine_ingredients, species_substance_safety, substances};
#[derive(Queryable, Selectable)]
#[diesel(check_for_backend(diesel::pg::Pg))]
pub struct SubstanceSafetyForSpecies {
#[diesel(select_expression =
species_substance_safety::maximum_milligrams_per_kilo_per_day *
medicine_ingredients::dosage_per_unit
)]
pub maximum_dose_per_kilo_per_day: f64,
#[diesel(select_expression =
species_substance_safety::milligrams_per_kilo_per_day *
medicine_ingredients::dosage_per_unit
)]
pub recommended_dose_per_kilo_per_day: f64,
}
fn main() {
let conn = &mut PgConnection::establish("_").unwrap();
let substance_ids = Vec::<uuid::Uuid>::new();
species_substance_safety::table
.inner_join(substances::table.inner_join(medicine_ingredients::table))
.filter(species_substance_safety::substance_id.eq_any(substance_ids))
.select(SubstanceSafetyForSpecies::as_select())
.load::<SubstanceSafetyForSpecies>(conn);
} |
Beta Was this translation helpful? Give feedback.
-
Thanks for replying. I get either:
or, if I add
Edit: just to add, I get the exact same errors if I extract the multiplication into a function: // `_` gets the first error, if I change the return type to `Float8`, I'll get the second error
#[diesel::dsl::auto_type]
fn calculate_max_dose() -> _ {
dosage_per_unit * maximum_milligrams_per_kilo_per_day
} |
Beta Was this translation helpful? Give feedback.
Thanks for providing these information. I tracked it down now. It's not the
diesel_derives
version, but thedsl_auto_type
version that's relevant. I can reproduce your error with version 0.1.2 or older. Version 0.1.3 fixes this by introducing support for this kind of expressions, so the solution to your problem is to executecargo update -p dsl_auto_type
to update that package. After that it will just work.