diff --git a/examples/sqlx-mysql/src/example_cake.rs b/examples/sqlx-mysql/src/example_cake.rs index 4a32fa56..475315e8 100644 --- a/examples/sqlx-mysql/src/example_cake.rs +++ b/examples/sqlx-mysql/src/example_cake.rs @@ -72,14 +72,4 @@ impl Related for Entity { } } -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) - } -} - impl ActiveModelBehavior for ActiveModel {} diff --git a/examples/sqlx-mysql/src/example_filling.rs b/examples/sqlx-mysql/src/example_filling.rs index d2dece8a..925b92fc 100644 --- a/examples/sqlx-mysql/src/example_filling.rs +++ b/examples/sqlx-mysql/src/example_filling.rs @@ -62,10 +62,4 @@ impl Related for Entity { } } -impl Model { - pub fn find_cake(&self) -> Select { - Entity::find_related().belongs_to::(self) - } -} - impl ActiveModelBehavior for ActiveModel {} diff --git a/examples/sqlx-mysql/src/select.rs b/examples/sqlx-mysql/src/select.rs index fc4f2d07..825efdab 100644 --- a/examples/sqlx-mysql/src/select.rs +++ b/examples/sqlx-mysql/src/select.rs @@ -159,7 +159,7 @@ async fn find_many_to_many(db: &DbConn) -> Result<(), QueryErr> { let cheese = Cake::find_by_id(1).one(db).await?; if let Some(cheese) = cheese { - let fillings: Vec = cheese.find_filling().all(db).await?; + let fillings: Vec = cheese.find_related(Filling).all(db).await?; println!(); for ff in fillings.iter() { @@ -172,7 +172,7 @@ async fn find_many_to_many(db: &DbConn) -> Result<(), QueryErr> { let lemon = Filling::find_by_id(2).one(db).await?; if let Some(lemon) = lemon { - let cakes: Vec = lemon.find_cake().all(db).await?; + let cakes: Vec = lemon.find_related(Cake).all(db).await?; println!(); for cc in cakes.iter() { diff --git a/src/database/connection.rs b/src/database/connection.rs index 96170099..95911c29 100644 --- a/src/database/connection.rs +++ b/src/database/connection.rs @@ -119,6 +119,7 @@ impl DatabaseConnection { None } + #[cfg(feature = "mock")] pub fn into_transaction_log(self) -> Vec { let mut mocker = self.as_mock_connection().get_mocker_mutex().lock().unwrap(); mocker.drain_transaction_log() diff --git a/src/entity/model.rs b/src/entity/model.rs index 81821c68..4419218a 100644 --- a/src/entity/model.rs +++ b/src/entity/model.rs @@ -1,4 +1,4 @@ -use crate::{EntityTrait, QueryResult, TypeErr}; +use crate::{Select, EntityTrait, Related, QueryFilter, QueryResult, TypeErr}; pub use sea_query::Value; use std::fmt::Debug; @@ -8,6 +8,13 @@ pub trait ModelTrait: Clone + Debug { fn get(&self, c: ::Column) -> Value; fn set(&mut self, c: ::Column, v: Value); + + fn find_related(&self, _: R) -> Select + where + R: EntityTrait, + Self::Entity: Related { + >::find_related().belongs_to(self) + } } pub trait FromQueryResult { diff --git a/src/query/helper.rs b/src/query/helper.rs index da01fab4..7044c63d 100644 --- a/src/query/helper.rs +++ b/src/query/helper.rs @@ -258,11 +258,11 @@ pub trait QueryFilter: Sized { } /// Apply a where condition using the model's primary key - fn belongs_to(mut self, model: &E::Model) -> Self + fn belongs_to(mut self, model: &M) -> Self where - E: EntityTrait, + M: ModelTrait, { - for key in E::PrimaryKey::iter() { + for key in ::PrimaryKey::iter() { let col = key.into_column(); self = self.filter(col.eq(model.get(col))); } diff --git a/src/query/join.rs b/src/query/join.rs index 12d7ef58..f15dcfe6 100644 --- a/src/query/join.rs +++ b/src/query/join.rs @@ -62,7 +62,7 @@ where #[cfg(test)] mod tests { use crate::tests_cfg::{cake, filling, fruit}; - use crate::{ColumnTrait, EntityTrait, QueryFilter, QueryTrait}; + use crate::{ColumnTrait, EntityTrait, QueryFilter, QueryTrait, ModelTrait}; use sea_query::MysqlQueryBuilder; #[test] @@ -139,7 +139,7 @@ mod tests { }; assert_eq!( - cake_model.find_fruit().build(MysqlQueryBuilder).to_string(), + cake_model.find_related(fruit::Entity).build(MysqlQueryBuilder).to_string(), [ "SELECT `fruit`.`id`, `fruit`.`name`, `fruit`.`cake_id` FROM `fruit`", "INNER JOIN `cake` ON `cake`.`id` = `fruit`.`cake_id`", diff --git a/src/tests_cfg/cake.rs b/src/tests_cfg/cake.rs index c5ba7348..f8a35d6c 100644 --- a/src/tests_cfg/cake.rs +++ b/src/tests_cfg/cake.rs @@ -73,14 +73,4 @@ impl Related for Entity { } } -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) - } -} - impl ActiveModelBehavior for ActiveModel {} diff --git a/src/tests_cfg/filling.rs b/src/tests_cfg/filling.rs index 9dbe78f1..a246d146 100644 --- a/src/tests_cfg/filling.rs +++ b/src/tests_cfg/filling.rs @@ -63,10 +63,4 @@ impl Related for Entity { } } -impl Model { - pub fn find_cake(&self) -> Select { - Entity::find_related().belongs_to::(self) - } -} - impl ActiveModelBehavior for ActiveModel {}