diff --git a/examples/cli/src/entity/cake.rs b/examples/cli/src/entity/cake.rs index 9b786a5f..12e7c4ab 100644 --- a/examples/cli/src/entity/cake.rs +++ b/examples/cli/src/entity/cake.rs @@ -71,13 +71,4 @@ impl Related for Entity { } } -impl Model { - pub fn find_cake_filling(&self) -> Select { - Entity::find_related().belongs_to::(self) - } - pub fn find_fruit(&self) -> Select { - Entity::find_related().belongs_to::(self) - } -} - impl ActiveModelBehavior for ActiveModel {} diff --git a/examples/cli/src/entity/cake_filling.rs b/examples/cli/src/entity/cake_filling.rs index d5b4b8b6..1ac64920 100644 --- a/examples/cli/src/entity/cake_filling.rs +++ b/examples/cli/src/entity/cake_filling.rs @@ -78,13 +78,4 @@ impl Related for Entity { } } -impl Model { - pub fn find_cake(&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/cli/src/entity/filling.rs b/examples/cli/src/entity/filling.rs index e4563e55..752317a4 100644 --- a/examples/cli/src/entity/filling.rs +++ b/examples/cli/src/entity/filling.rs @@ -63,10 +63,4 @@ impl Related for Entity { } } -impl Model { - pub fn find_cake_filling(&self) -> Select { - Entity::find_related().belongs_to::(self) - } -} - impl ActiveModelBehavior for ActiveModel {} diff --git a/examples/cli/src/entity/fruit.rs b/examples/cli/src/entity/fruit.rs index f65d5b38..7ff7d9b8 100644 --- a/examples/cli/src/entity/fruit.rs +++ b/examples/cli/src/entity/fruit.rs @@ -39,6 +39,7 @@ impl PrimaryKeyTrait for PrimaryKey { #[derive(Copy, Clone, Debug, EnumIter)] pub enum Relation { Cake, + Vendor, } impl ColumnTrait for Column { @@ -59,6 +60,7 @@ impl RelationTrait for Relation { .from(Column::CakeId) .to(super::cake::Column::Id) .into(), + Self::Vendor => Entity::has_many(super::vendor::Entity).into(), } } } @@ -69,9 +71,9 @@ impl Related for Entity { } } -impl Model { - pub fn find_cake(&self) -> Select { - Entity::find_related().belongs_to::(self) +impl Related for Entity { + fn to() -> RelationDef { + Relation::Vendor.def() } } diff --git a/examples/cli/src/entity/mod.rs b/examples/cli/src/entity/mod.rs index 006f7e80..395d29f9 100644 --- a/examples/cli/src/entity/mod.rs +++ b/examples/cli/src/entity/mod.rs @@ -4,3 +4,4 @@ pub mod cake; pub mod cake_filling; pub mod filling; pub mod fruit; +pub mod vendor; diff --git a/examples/cli/src/entity/prelude.rs b/examples/cli/src/entity/prelude.rs index b4c1c94f..b4e85c78 100644 --- a/examples/cli/src/entity/prelude.rs +++ b/examples/cli/src/entity/prelude.rs @@ -4,3 +4,4 @@ pub use super::cake::Entity as Cake; pub use super::cake_filling::Entity as CakeFilling; pub use super::filling::Entity as Filling; pub use super::fruit::Entity as Fruit; +pub use super::vendor::Entity as Vendor; diff --git a/examples/cli/src/entity/vendor.rs b/examples/cli/src/entity/vendor.rs index 9c4ca7dd..2262519f 100644 --- a/examples/cli/src/entity/vendor.rs +++ b/examples/cli/src/entity/vendor.rs @@ -47,7 +47,7 @@ impl ColumnTrait for Column { match self { Self::Id => ColumnType::Integer.def(), Self::Name => ColumnType::String(Some(255u32)).def(), - Self::FruitId => ColumnType::Integer.def(), + Self::FruitId => ColumnType::Integer.def().null(), } } } @@ -55,7 +55,7 @@ impl ColumnTrait for Column { impl RelationTrait for Relation { fn def(&self) -> RelationDef { match self { - Self::Fruit => Entity::has_one(super::fruit::Entity) + Self::Fruit => Entity::belongs_to(super::fruit::Entity) .from(Column::FruitId) .to(super::fruit::Column::Id) .into(), @@ -69,10 +69,4 @@ impl Related for Entity { } } -impl Model { - pub fn find_fruit(&self) -> Select { - Entity::find_related().belongs_to::(self) - } -} - impl ActiveModelBehavior for ActiveModel {} diff --git a/examples/codegen/src/entity/cake.rs b/examples/codegen/src/entity/cake.rs index 87ee5f1a..12e7c4ab 100644 --- a/examples/codegen/src/entity/cake.rs +++ b/examples/codegen/src/entity/cake.rs @@ -53,14 +53,8 @@ impl ColumnTrait for Column { impl RelationTrait for Relation { fn def(&self) -> RelationDef { match self { - Self::CakeFilling => Entity::has_many(super::cake_filling::Entity) - .from(Column::Id) - .to(super::cake_filling::Column::CakeId) - .into(), - Self::Fruit => Entity::has_many(super::fruit::Entity) - .from(Column::Id) - .to(super::fruit::Column::CakeId) - .into(), + Self::CakeFilling => Entity::has_many(super::cake_filling::Entity).into(), + Self::Fruit => Entity::has_many(super::fruit::Entity).into(), } } } @@ -77,13 +71,4 @@ impl Related for Entity { } } -impl Model { - pub fn find_cake_filling(&self) -> Select { - Entity::find_related().belongs_to::(self) - } - pub fn find_fruit(&self) -> Select { - Entity::find_related().belongs_to::(self) - } -} - impl ActiveModelBehavior for ActiveModel {} diff --git a/examples/codegen/src/entity/cake_filling.rs b/examples/codegen/src/entity/cake_filling.rs index b35279d4..1ac64920 100644 --- a/examples/codegen/src/entity/cake_filling.rs +++ b/examples/codegen/src/entity/cake_filling.rs @@ -54,11 +54,11 @@ impl ColumnTrait for Column { impl RelationTrait for Relation { fn def(&self) -> RelationDef { match self { - Self::Cake => Entity::has_one(super::cake::Entity) + Self::Cake => Entity::belongs_to(super::cake::Entity) .from(Column::CakeId) .to(super::cake::Column::Id) .into(), - Self::Filling => Entity::has_one(super::filling::Entity) + Self::Filling => Entity::belongs_to(super::filling::Entity) .from(Column::FillingId) .to(super::filling::Column::Id) .into(), @@ -78,13 +78,4 @@ impl Related for Entity { } } -impl Model { - pub fn find_cake(&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/codegen/src/entity/filling.rs b/examples/codegen/src/entity/filling.rs index 4134652f..752317a4 100644 --- a/examples/codegen/src/entity/filling.rs +++ b/examples/codegen/src/entity/filling.rs @@ -52,10 +52,7 @@ impl ColumnTrait for Column { impl RelationTrait for Relation { fn def(&self) -> RelationDef { match self { - Self::CakeFilling => Entity::has_many(super::cake_filling::Entity) - .from(Column::Id) - .to(super::cake_filling::Column::FillingId) - .into(), + Self::CakeFilling => Entity::has_many(super::cake_filling::Entity).into(), } } } @@ -66,10 +63,4 @@ impl Related for Entity { } } -impl Model { - pub fn find_cake_filling(&self) -> Select { - Entity::find_related().belongs_to::(self) - } -} - impl ActiveModelBehavior for ActiveModel {} diff --git a/examples/codegen/src/entity/fruit.rs b/examples/codegen/src/entity/fruit.rs index d3f21bf9..7ff7d9b8 100644 --- a/examples/codegen/src/entity/fruit.rs +++ b/examples/codegen/src/entity/fruit.rs @@ -48,7 +48,7 @@ impl ColumnTrait for Column { match self { Self::Id => ColumnType::Integer.def(), Self::Name => ColumnType::String(Some(255u32)).def(), - Self::CakeId => ColumnType::Integer.def(), + Self::CakeId => ColumnType::Integer.def().null(), } } } @@ -56,14 +56,11 @@ impl ColumnTrait for Column { impl RelationTrait for Relation { fn def(&self) -> RelationDef { match self { - Self::Cake => Entity::has_one(super::cake::Entity) + Self::Cake => Entity::belongs_to(super::cake::Entity) .from(Column::CakeId) .to(super::cake::Column::Id) .into(), - Self::Vendor => Entity::has_many(super::vendor::Entity) - .from(Column::Id) - .to(super::vendor::Column::FruitId) - .into(), + Self::Vendor => Entity::has_many(super::vendor::Entity).into(), } } } @@ -80,13 +77,4 @@ impl Related for Entity { } } -impl Model { - pub fn find_cake(&self) -> Select { - Entity::find_related().belongs_to::(self) - } - pub fn find_vendor(&self) -> Select { - Entity::find_related().belongs_to::(self) - } -} - impl ActiveModelBehavior for ActiveModel {} diff --git a/examples/codegen/src/entity/vendor.rs b/examples/codegen/src/entity/vendor.rs index 9c4ca7dd..2262519f 100644 --- a/examples/codegen/src/entity/vendor.rs +++ b/examples/codegen/src/entity/vendor.rs @@ -47,7 +47,7 @@ impl ColumnTrait for Column { match self { Self::Id => ColumnType::Integer.def(), Self::Name => ColumnType::String(Some(255u32)).def(), - Self::FruitId => ColumnType::Integer.def(), + Self::FruitId => ColumnType::Integer.def().null(), } } } @@ -55,7 +55,7 @@ impl ColumnTrait for Column { impl RelationTrait for Relation { fn def(&self) -> RelationDef { match self { - Self::Fruit => Entity::has_one(super::fruit::Entity) + Self::Fruit => Entity::belongs_to(super::fruit::Entity) .from(Column::FruitId) .to(super::fruit::Column::Id) .into(), @@ -69,10 +69,4 @@ impl Related for Entity { } } -impl Model { - pub fn find_fruit(&self) -> Select { - Entity::find_related().belongs_to::(self) - } -} - impl ActiveModelBehavior for ActiveModel {} diff --git a/sea-orm-codegen/src/entity/writer.rs b/sea-orm-codegen/src/entity/writer.rs index 01dd4a4c..3bd8fb59 100644 --- a/sea-orm-codegen/src/entity/writer.rs +++ b/sea-orm-codegen/src/entity/writer.rs @@ -117,7 +117,6 @@ impl EntityWriter { ]; code_blocks.extend(Self::gen_impl_related(entity)); code_blocks.extend(vec![ - Self::gen_impl_model(entity), Self::gen_impl_active_model_behavior(), ]); code_blocks @@ -256,18 +255,6 @@ impl EntityWriter { .collect() } - pub fn gen_impl_model(entity: &Entity) -> TokenStream { - let relation_ref_tables_snake_case = entity.get_relation_ref_tables_snake_case(); - let relation_rel_find_helpers = entity.get_relation_rel_find_helpers(); - quote! { - impl Model { - #(pub fn #relation_rel_find_helpers(&self) -> Select { - Entity::find_related().belongs_to::(self) - })* - } - } - } - pub fn gen_impl_active_model_behavior() -> TokenStream { quote! { impl ActiveModelBehavior for ActiveModel {}