use sea_orm::entity::prelude::*; #[derive(Copy, Clone, Default, Debug, DeriveEntity)] #[table = "cake"] pub struct Entity; #[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)] pub struct Model { pub id: i32, pub name: String, } #[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)] pub enum Column { Id, Name, } #[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)] pub enum PrimaryKey { Id, } #[derive(Copy, Clone, Debug, EnumIter)] pub enum Relation { Fruit, } impl ColumnTrait for Column { type EntityName = Entity; fn def(&self) -> ColumnType { match self { Self::Id => ColumnType::Integer(None), Self::Name => ColumnType::String(None), } } } impl RelationTrait for Relation { fn def(&self) -> RelationDef { match self { Self::Fruit => Entity::has_many(super::fruit::Entity) .from(Column::Id) .to(super::fruit::Column::CakeId) .into(), } } } impl Related for Entity { fn to() -> RelationDef { Relation::Fruit.def() } } impl Related for Entity { fn to() -> RelationDef { super::cake_filling::Relation::Filling.def() } fn via() -> Option { Some(super::cake_filling::Relation::Cake.def().rev()) } } impl Model { pub fn find_fruit(&self) -> Select { Entity::find_related().belongs_to::(self) } pub fn find_filling(&self) -> Select { Entity::find_related().belongs_to::(self) } }