diff --git a/examples/sqlx-mysql/src/example_cake.rs b/examples/sqlx-mysql/src/example_cake.rs index d8c43a10..755efd6a 100644 --- a/examples/sqlx-mysql/src/example_cake.rs +++ b/examples/sqlx-mysql/src/example_cake.rs @@ -24,7 +24,6 @@ pub enum PrimaryKey { #[derive(Copy, Clone, Debug, EnumIter)] pub enum Relation { Fruit, - CakeFilling, } impl ColumnTrait for Column { @@ -45,10 +44,6 @@ impl RelationTrait for Relation { .from(Column::Id) .to(super::fruit::Column::CakeId) .into(), - Self::CakeFilling => Entity::has_many(super::cake_filling::Entity) - .from(Column::Id) - .to(super::cake_filling::Column::CakeId) - .into(), } } } @@ -65,7 +60,7 @@ impl Related for Entity { } fn via() -> Option { - Some(Relation::CakeFilling.def()) + Some(super::cake_filling::Relation::Cake.def().rev()) } } @@ -73,4 +68,8 @@ 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) + } } diff --git a/examples/sqlx-mysql/src/example_cake_filling.rs b/examples/sqlx-mysql/src/example_cake_filling.rs index 66dad79a..9812f57a 100644 --- a/examples/sqlx-mysql/src/example_cake_filling.rs +++ b/examples/sqlx-mysql/src/example_cake_filling.rs @@ -42,11 +42,11 @@ impl ColumnTrait for Column { impl RelationTrait for Relation { fn def(&self) -> RelationDef { match self { - Self::Cake => Entity::has_many(super::cake::Entity) + Self::Cake => Entity::has_one(super::cake::Entity) .from(Column::CakeId) .to(super::cake::Column::Id) .into(), - Self::Filling => Entity::has_many(super::filling::Entity) + Self::Filling => Entity::has_one(super::filling::Entity) .from(Column::FillingId) .to(super::filling::Column::Id) .into(), diff --git a/examples/sqlx-mysql/src/example_filling.rs b/examples/sqlx-mysql/src/example_filling.rs index faa2872a..45142363 100644 --- a/examples/sqlx-mysql/src/example_filling.rs +++ b/examples/sqlx-mysql/src/example_filling.rs @@ -40,3 +40,19 @@ impl RelationTrait for Relation { panic!() } } + +impl Related for Entity { + fn to() -> RelationDef { + super::cake_filling::Relation::Cake.def() + } + + fn via() -> Option { + Some(super::cake_filling::Relation::Filling.def().rev()) + } +} + +impl Model { + pub fn find_cake(&self) -> Select { + Entity::find_related().belongs_to::(self) + } +} diff --git a/examples/sqlx-mysql/src/main.rs b/examples/sqlx-mysql/src/main.rs index a0dfc41b..6061335a 100644 --- a/examples/sqlx-mysql/src/main.rs +++ b/examples/sqlx-mysql/src/main.rs @@ -150,5 +150,27 @@ async fn find_many_to_many(db: &Database) -> Result<(), QueryErr> { println!("{:?}\n", bb); } + print!("find fillings for cheese cake: "); + + let cheese = cake::Entity::find_by(1).one(db).await?; + + let fillings: Vec = cheese.find_filling().all(db).await?; + + println!(); + for ff in fillings.iter() { + println!("{:?}\n", ff); + } + + print!("find cakes for lemon: "); + + let lemon = filling::Entity::find_by(2).one(db).await?; + + let cakes: Vec = lemon.find_cake().all(db).await?; + + println!(); + for cc in cakes.iter() { + println!("{:?}\n", cc); + } + Ok(()) } diff --git a/src/entity/relation.rs b/src/entity/relation.rs index 1579b73a..1626f2cc 100644 --- a/src/entity/relation.rs +++ b/src/entity/relation.rs @@ -50,6 +50,19 @@ where to_col: Option, } +impl RelationDef { + /// Reverse this relation (swap from and to) + pub fn rev(self) -> Self { + Self { + rel_type: self.rel_type, + from_tbl: self.to_tbl, + to_tbl: self.from_tbl, + from_col: self.to_col, + to_col: self.from_col, + } + } +} + impl RelationBuilder where E: EntityTrait, diff --git a/src/tests_cfg/cake.rs b/src/tests_cfg/cake.rs index d8fb319d..56b2742c 100644 --- a/src/tests_cfg/cake.rs +++ b/src/tests_cfg/cake.rs @@ -25,7 +25,6 @@ pub enum PrimaryKey { #[derive(Copy, Clone, Debug, EnumIter)] pub enum Relation { Fruit, - CakeFilling, } impl ColumnTrait for Column { @@ -46,10 +45,6 @@ impl RelationTrait for Relation { .from(Column::Id) .to(super::fruit::Column::CakeId) .into(), - Self::CakeFilling => Entity::has_many(super::cake_filling::Entity) - .from(Column::Id) - .to(super::cake_filling::Column::CakeId) - .into(), } } } @@ -66,7 +61,7 @@ impl Related for Entity { } fn via() -> Option { - Some(Relation::CakeFilling.def()) + Some(super::cake_filling::Relation::Cake.def().rev()) } } @@ -74,4 +69,8 @@ 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) + } } diff --git a/src/tests_cfg/cake_filling.rs b/src/tests_cfg/cake_filling.rs index c9058e78..b4c2f95e 100644 --- a/src/tests_cfg/cake_filling.rs +++ b/src/tests_cfg/cake_filling.rs @@ -43,11 +43,11 @@ impl ColumnTrait for Column { impl RelationTrait for Relation { fn def(&self) -> RelationDef { match self { - Self::Cake => Entity::has_many(super::cake::Entity) + Self::Cake => Entity::has_one(super::cake::Entity) .from(Column::CakeId) .to(super::cake::Column::Id) .into(), - Self::Filling => Entity::has_many(super::filling::Entity) + Self::Filling => Entity::has_one(super::filling::Entity) .from(Column::FillingId) .to(super::filling::Column::Id) .into(), diff --git a/src/tests_cfg/filling.rs b/src/tests_cfg/filling.rs index ee51b5b4..aecf23e0 100644 --- a/src/tests_cfg/filling.rs +++ b/src/tests_cfg/filling.rs @@ -41,3 +41,19 @@ impl RelationTrait for Relation { panic!() } } + +impl Related for Entity { + fn to() -> RelationDef { + super::cake_filling::Relation::Cake.def() + } + + fn via() -> Option { + Some(super::cake_filling::Relation::Filling.def().rev()) + } +} + +impl Model { + pub fn find_cake(&self) -> Select { + Entity::find_related().belongs_to::(self) + } +}