-
|
Hello, struct User {
active: bool,
credentials: UserCredentials
};
struct UserCredentials {
username: String,
password: String,
};(I know this example doesn't necessarily need the nested items but lets say for the sake of example.) // Entity/user.rs
#[sea_orm(table_name = "user")]
struct Model {
#[sea_orm(primary_key)]
id: i32,
active: bool,
credentials_id: i32
};and // Entity/user_credentials.rs
#[sea_orm(table_name = "user_credentials")]
struct Model {
#[sea_orm(primary_key)]
id: i32,
username: String,
password: String,
};I can then (if I understand the documentation correctly) create a new struct in my regular code as follows: // main.rs
#[derive( DerivePartialModel)]
#[sea_orm(entity = "User")]
struct UserCopy {
active: bool,
#[sea_orm(nested)]
credentials: entity::prelude::user::Model
};If I then add an entry in my database and execute let user: UserCopy = User::find_by_id(1).left_join(Credentials).into_partial_model().one(&db).await.unwrap().unwrap();To get the desired result of a struct with a full nested struct inside it. This approach works, however I don't like that I have to make sure two versions of the same entity are up to date, the migration version, which creates the entity. And the nestable version with exactly the same fields as the other one. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
|
I think you can define it like this: #[derive(DerivePartialModel)]
#[sea_orm(entity = "user::Entity")]
struct UserWithCredentials {
#[sea_orm(nested)]
user: user::Model,
#[sea_orm(nested)]
credentials: UserCredentials,
} |
Beta Was this translation helpful? Give feedback.
-
|
Hi @tyt2y3, Thanks for your response. That sollution does indeed work, you would have to type Lets say I have the model // Entity/user.rs
#[sea_orm(table_name = "user")]
struct Model {
#[sea_orm(primary_key)]
id: i32,
active: bool,
credentials_id: i32,
email_smtp_id: i32,
};
// Entity/user_credentials.rs
#[sea_orm(table_name = "user_credentials")]
struct Model {
#[sea_orm(primary_key)]
id: i32,
username: String,
password: String,
password_reset_smtp_id: i32,
};
// Entity/email_properties.rs
#[sea_orm(table_name = "email_properties")]
struct Model {
#[sea_orm(primary_key)]
id: i32,
smtp_username: String,
smtp_password: String,
};and using your sollution create the following structs #[derive( DerivePartialModel)]
#[sea_orm(entity = "User")]
struct UserNested {
#[sea_orm(nested)]
user: user::Model,
#[sea_orm(nested)]
user_credentials: CredentialsNested,
#[sea_orm(nested)]
email_smtp: email_properties::Model
};
#[derive( DerivePartialModel)]
#[sea_orm(entity = "email_properties")]
struct CredentialsNested {
#[sea_orm(nested)]
user_credentials: user_credentials::Model,
#[sea_orm(nested)]
email_smtp: email_properties::Model
};If I create in my database two columns of email_properties and assign ID 1 to the User and ID 2 to the UserCredentials. If I execute and print {
"user": {
"id": 1,
"active": false,
"credentials_id": 1,
"email_smtp_id": 1
},
"user_credentials": {
"user_credentials": {
"id": 1,
"smtp_username": "user1",
"smtp_password": "qwerty",
"email_properties_id": 2
},
"email_properties": {
"id": 1,
"smtp_username": "[email protected]",
"smtp_password": "qwerty"
}
},
"email_properties": {
"id": 1,
"smtp_username": "[email protected]",
"smtp_password": "qwerty"
}
}I am sure I did something wrong in my approach, and maybe my usecase is incredibly cursed. But I would really love if this would be possible. Is it currently possible at all what I want? |
Beta Was this translation helpful? Give feedback.
I think you can define it like this: