Hi again, it's me !
I am lost with diesel ORM...
I am trying to select a user, with all agencies link to him.
But I don't success to make it works...
I've tried to follow some topics, like this one, or this.
But I think I misunderstand some things...
shema.rs
table! {
agencies (id) {
id -> Varchar,
label -> Varchar,
mail -> Varchar,
uri -> Varchar,
logo -> Varchar,
}
}
table! {
users (id) {
id -> Int4,
mail -> Varchar,
pass -> Bpchar,
}
}
table! {
users_agencies (users_id, agencies_id) {
users_id -> Int4,
agencies_id -> Varchar,
}
}
joinable!(users_agencies -> agencies (agencies_id));
joinable!(users_agencies -> users (users_id));
allow_tables_to_appear_in_same_query!(
agencies,
users,
users_agencies,
);
models.rs
use diesel::PgConnection;
use super::schema::*;
use crate::business::{agency::Agency, error::ApiError, user::ApiUser};
#[derive(Identifiable, Insertable, Queryable, PartialEq, Debug)]
#[table_name = "users"]
pub struct Users {
pub id: i32,
pub mail: String,
pub pass: String,
}
impl Users {
pub fn into_api_user(&self, db_connection: &PgConnection) -> Result<ApiUser, ApiError> {
match UsersAgencies::belonging_to(self)
.inner_join(agencies::table)
.select(agencies::all_columns)
.load::<Agencies>(db_connection)
{
Ok(agencies) => Ok(ApiUser::new(
self.mail,
agencies.into_iter().map(|a| a.into_agency).collect(),
)),
Err(e) => Err(ApiError::new(format!(
"Error while converting db Users into ApiUser : {}",
e
))),
}
}
}
#[derive(Insertable, Queryable, PartialEq, Debug)]
#[table_name = "agencies"]
pub struct Agencies {
pub id: String,
pub label: String,
pub mail: String,
pub uri: String,
pub logo: String,
}
impl Agencies {
pub fn into_agency(&self) -> Agency {
Agency::new(self.id, self.label, self.mail, self.uri, self.logo)
}
}
#[derive(Queryable, Associations, PartialEq, Debug)]
#[belongs_to(Users)]
#[belongs_to(Agencies)]
#[table_name = "users_agencies"]
pub struct UsersAgencies {
pub users_id: String,
pub agencies_id: String,
}
implementation
#[macro_use]
extern crate diesel;
extern crate diesel_codegen;
extern crate r2d2;
extern crate r2d2_diesel;
use diesel::prelude::*;
use diesel::PgConnection;
use crate::business::{ad::Ad, error::ApiError, user::ApiUser};
use super::db_models::models::Users;
use super::db_models::schema::users::dsl::*;
pub fn authenticate(
&self,
login: String,
password: String,
db_connection: &PgConnection,
) -> Result<ApiUser, ApiError> {
let usrs = match users
.filter(mail.eq(&login))
.filter(pass.eq(self.hash_password(&password)))
.load::<Users>(db_connection)
{
Ok(u) => u,
Err(e) => {
return Err(ApiError::new(format!(
"User '{}' unauthorized : {}",
login, e
)))
}
};
match usrs.get(0) {
Some(u) => u.into_api_user(db_connection),
None => Err(ApiError::new(format!("No user found for '{}'", login))),
}
}
Build error
error[E0599]: no function or associated item named `belonging_to` found for struct `UsersAgencies` in the current scope
--> src\infrastructure\db_models\models.rs:16:30
|
16 | match UsersAgencies::belonging_to(self)
| ^^^^^^^^^^^^ function or associated item not found in `UsersAgencies`
...
53 | pub struct UsersAgencies {
| ------------------------ function or associated item `belonging_to` not found for this
|
= help: items from traits can only be used if the trait is implemented and in scope
= note: the following trait defines an item `belonging_to`, perhaps you need to implement it:
candidate #1: `BelongingToDsl`
I tried to implement 'BelongingToDsl' trait on UsersAgencies, but I don't understand how to fill the function.
Does soemone can help me again please ?