About supporting vec<partial_model> in partial model #2539
Unanswered
Silence-kzp
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I want to define a channel vec in the group partial model, and then use load_many in the query to assign values to channels. When I use the following code, I get a prompt:
Error
the trait boundlist::Listmodels::channel::Model: PartialModelTraitis not satisfied the following other types implement traitPartialModelTrait: Bill Country GroupCategory models::group::Group std::option::Option<T> required forstd::option::Option<list::Listmodels::channel::Model>to implementPartialModelTrait``Model
`
/// List
#[derive(Clone, Debug, Default, PartialEq, Deserialize, Serialize)]
#[serde(transparent)]
pub struct List(pub Vec);
impl sea_orm::TryGetableFromJson for List where for<'de> T: Deserialize<'de> {}
impl std::convert::From<List> for sea_orm::Value
where
List: Serialize,
{
fn from(source: List) -> Self {
sea_orm::Value::Json(
serde_json::to_value(&source)
.ok()
.map(|s| std::boxed::Box::new(s)),
)
}
}
impl sea_orm::sea_query::ValueType for List
where
List: DeserializeOwned,
{
fn try_from(v: sea_orm::Value) -> Result<Self, sea_orm::sea_query::ValueTypeErr> {
match v {
sea_orm::Value::Json(Some(json)) => {
Ok(serde_json::from_value(*json).map_err(|_| sea_orm::sea_query::ValueTypeErr)?)
}
_ => Err(sea_orm::sea_query::ValueTypeErr),
}
}
}
impl sea_orm::sea_query::Nullable for List {
fn null() -> sea_orm::Value {
sea_orm::Value::Json(None)
}
}
/// Group
#[derive(Debug, Clone, Deserialize, Serialize, DerivePartialModel)]
#[sea_orm(entity = "Entity", from_query_result, into_active_model)]
pub struct Group {
pub id: i64,
pub name: String,
}
/// Channel
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, DerivePartialModel)]
#[sea_orm(entity = "Entity", from_query_result, into_active_model)]
pub struct Channel {
pub id: i64,
pub name: String,
}
/// Group And Channel many to many
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name="group_channels")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub group_id: i64,
#[sea_orm(primary_key, auto_increment = false)]
pub channel_id: i64,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::group::Entity",
from = "Column::GroupId",
to = "super::group::Column::Id"
)]
Group,
#[sea_orm(
belongs_to = "super::channel::Entity",
from = "Column::ChannelId",
to = "super::channel::Column::Id"
)]
Channel,
}
impl ActiveModelBehavior for ActiveModel {}
`
Beta Was this translation helpful? Give feedback.
All reactions