-
Hi, SELECT model.*, brand.*, base_model.*, base_brand.* FROM model
LEFT JOIN brand ON model.brand_id = brand.id
LEFT JOIN model as base_model ON model.model_id = base_model.id
LEFT JOIN brand as base_brand ON base_model.brand_id = base_brand.id; As you can see the model left joins itself once. #[derive(Queryable, Selectable, Identifiable, PartialEq, Debug, Associations)]
#[diesel(belongs_to(Model, foreign_key = model_id))]
#[diesel(belongs_to(super::Brand))]
#[diesel(table_name = crate::schema::model)]
#[diesel(check_for_backend(diesel::pg::Pg))]
pub struct Model {
pub id: Uuid,
pub brand_id: Option<Uuid>,
pub model_id: Option<Uuid>,
pub name: String,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
pub deleted_at: Option<DateTime<Utc>>,
}
#[derive(Queryable, Selectable, Identifiable, PartialEq, Debug)]
#[diesel(table_name = crate::schema::brand)]
#[diesel(check_for_backend(diesel::pg::Pg))]
pub struct Brand {
pub id: Uuid,
pub name: String,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
pub deleted_at: Option<DateTime<Utc>>,
}
diesel::alias! {
pub const BASE_MODEL: Alias<base_model> = crate::schema::model as base_model;
pub const BASE_BRAND: Alias<base_brand> = crate::schema::brand as base_brand;
}
use crate::schema::brand as schema_brand;
use crate::schema::model as schema;
let mut query = schema::dsl::model
.left_join(schema_brand::table)
.left_join(BASE_MODEL.on(schema::model_id.eq(BASE_MODEL.field(schema::id).nullable())))
.left_join(
BASE_BRAND.on(BASE_MODEL
.field(schema::brand_id)
.eq(BASE_BRAND.field(schema_brand::id).nullable())),
)
.select((
Model::as_select(),
Option::<Brand>::as_select(),
BASE_MODEL.fields(Model::construct_selection()).nullable(), // <-- here is the problem
BASE_BRAND.fields(Brand::construct_selection()).nullable(), // <-- here is the problem
))
.into_boxed();
// Apply the filters dinamically here
type QueryResult = (Model, Option<Brand>, Option<Model>, Option<Brand>);
let mut conn = self.state.pool().get().await?;
let result: Vec<QueryResult> = query.load(&mut conn).await?; The left joins work well, and if I don't select the two aliases, the filters I apply later (conditionally) work well. My problem is to actually get the models back from the query. The above example gives me this error: ├╴ type annotations needed
│ cannot satisfy `_: diesel::backend::Backend`
│ the trait `diesel::backend::Backend` is implemented for `diesel::pg::Pg` rustc (E0283) Where the The diesel version I use: diesel = { version = "2.2.6", features = [
"chrono",
"numeric",
"serde_json",
"uuid",
] } I tried to search everywhere in the repo, on Google, I even tried to ask ChatGippity for help but nothing worked. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Rustc is not able to infer the correct backend type in this case. You should be able to solve this by using UFCS like this: For future questions: Please always provide the complete error message as emitted by the compiler (and not some part provided by your IDE). |
Beta Was this translation helpful? Give feedback.
Rustc is not able to infer the correct backend type in this case. You should be able to solve this by using UFCS like this:
<Model as Selectable<diesel::pg::Pg>>::construct_selection
For future questions: Please always provide the complete error message as emitted by the compiler (and not some part provided by your IDE).