From dcf044f2a127cd97f97c0807c8e176accc93e44d Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Thu, 8 Sep 2022 15:59:27 +0800 Subject: [PATCH] `DeriveRelation` on empty Relation enum --- examples/axum_example/entity/src/post.rs | 8 +------- examples/graphql_example/entity/src/note.rs | 8 +------- issues/471/src/post.rs | 8 +------- issues/630/src/entity/underscores.rs | 8 +------- issues/630/src/entity/underscores_workaround.rs | 8 +------- sea-orm-codegen/src/entity/writer.rs | 10 +--------- sea-orm-codegen/tests/compact/filling.rs | 8 +------- .../tests/compact_with_schema_name/filling.rs | 8 +------- src/tests_cfg/lunch_set.rs | 8 +------- 9 files changed, 9 insertions(+), 65 deletions(-) diff --git a/examples/axum_example/entity/src/post.rs b/examples/axum_example/entity/src/post.rs index 76d78196a..66a7b652f 100644 --- a/examples/axum_example/entity/src/post.rs +++ b/examples/axum_example/entity/src/post.rs @@ -14,13 +14,7 @@ pub struct Model { pub text: String, } -#[derive(Copy, Clone, Debug, EnumIter)] +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] pub enum Relation {} -impl RelationTrait for Relation { - fn def(&self) -> RelationDef { - panic!("No RelationDef") - } -} - impl ActiveModelBehavior for ActiveModel {} diff --git a/examples/graphql_example/entity/src/note.rs b/examples/graphql_example/entity/src/note.rs index 46f0c49be..a03f6d3f8 100644 --- a/examples/graphql_example/entity/src/note.rs +++ b/examples/graphql_example/entity/src/note.rs @@ -13,15 +13,9 @@ pub struct Model { pub text: String, } -#[derive(Copy, Clone, Debug, EnumIter)] +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] pub enum Relation {} -impl RelationTrait for Relation { - fn def(&self) -> RelationDef { - panic!("No RelationDef") - } -} - impl ActiveModelBehavior for ActiveModel {} impl Entity { diff --git a/issues/471/src/post.rs b/issues/471/src/post.rs index 3bb4d6a33..1e2e60463 100644 --- a/issues/471/src/post.rs +++ b/issues/471/src/post.rs @@ -14,13 +14,7 @@ pub struct Model { pub text: String, } -#[derive(Copy, Clone, Debug, EnumIter)] +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] pub enum Relation {} -impl RelationTrait for Relation { - fn def(&self) -> RelationDef { - panic!("No RelationDef") - } -} - impl ActiveModelBehavior for ActiveModel {} diff --git a/issues/630/src/entity/underscores.rs b/issues/630/src/entity/underscores.rs index ddd0be855..4dd20ac14 100644 --- a/issues/630/src/entity/underscores.rs +++ b/issues/630/src/entity/underscores.rs @@ -15,15 +15,9 @@ pub struct Model { pub aa_b_c_d: i32, } -#[derive(Copy, Clone, Debug, EnumIter)] +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] pub enum Relation {} -impl RelationTrait for Relation { - fn def(&self) -> RelationDef { - panic!("No RelationDef") - } -} - impl ActiveModelBehavior for ActiveModel {} #[cfg(test)] diff --git a/issues/630/src/entity/underscores_workaround.rs b/issues/630/src/entity/underscores_workaround.rs index 6773ad15f..68cc01089 100644 --- a/issues/630/src/entity/underscores_workaround.rs +++ b/issues/630/src/entity/underscores_workaround.rs @@ -20,15 +20,9 @@ pub struct Model { pub aa_b_c_d: i32, } -#[derive(Copy, Clone, Debug, EnumIter)] +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] pub enum Relation {} -impl RelationTrait for Relation { - fn def(&self) -> RelationDef { - panic!("No RelationDef") - } -} - impl ActiveModelBehavior for ActiveModel {} #[cfg(test)] diff --git a/sea-orm-codegen/src/entity/writer.rs b/sea-orm-codegen/src/entity/writer.rs index 19722e183..f9d6c7549 100644 --- a/sea-orm-codegen/src/entity/writer.rs +++ b/sea-orm-codegen/src/entity/writer.rs @@ -282,16 +282,8 @@ impl EntityWriter { let mut code_blocks = vec![ imports, Self::gen_compact_model_struct(entity, with_serde, date_time_crate, schema_name), + Self::gen_compact_relation_enum(entity), ]; - let relation_defs = if entity.get_relation_enum_name().is_empty() { - vec![ - Self::gen_relation_enum(entity), - Self::gen_impl_relation_trait(entity), - ] - } else { - vec![Self::gen_compact_relation_enum(entity)] - }; - code_blocks.extend(relation_defs); code_blocks.extend(Self::gen_impl_related(entity)); code_blocks.extend(Self::gen_impl_conjunct_related(entity)); code_blocks.extend(vec![Self::gen_impl_active_model_behavior()]); diff --git a/sea-orm-codegen/tests/compact/filling.rs b/sea-orm-codegen/tests/compact/filling.rs index dfedb1a77..de92558ea 100644 --- a/sea-orm-codegen/tests/compact/filling.rs +++ b/sea-orm-codegen/tests/compact/filling.rs @@ -10,15 +10,9 @@ pub struct Model { pub name: String, } -#[derive(Copy, Clone, Debug, EnumIter)] +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] pub enum Relation {} -impl RelationTrait for Relation { - fn def(&self) -> RelationDef { - panic!("No RelationDef") - } -} - impl Related for Entity { fn to() -> RelationDef { super::cake_filling::Relation::Cake.def() diff --git a/sea-orm-codegen/tests/compact_with_schema_name/filling.rs b/sea-orm-codegen/tests/compact_with_schema_name/filling.rs index 94795811e..ead70acd9 100644 --- a/sea-orm-codegen/tests/compact_with_schema_name/filling.rs +++ b/sea-orm-codegen/tests/compact_with_schema_name/filling.rs @@ -10,15 +10,9 @@ pub struct Model { pub name: String, } -#[derive(Copy, Clone, Debug, EnumIter)] +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] pub enum Relation {} -impl RelationTrait for Relation { - fn def(&self) -> RelationDef { - panic!("No RelationDef") - } -} - impl Related for Entity { fn to() -> RelationDef { super::cake_filling::Relation::Cake.def() diff --git a/src/tests_cfg/lunch_set.rs b/src/tests_cfg/lunch_set.rs index b063245ef..c0665da5a 100644 --- a/src/tests_cfg/lunch_set.rs +++ b/src/tests_cfg/lunch_set.rs @@ -11,13 +11,7 @@ pub struct Model { pub tea: Tea, } -#[derive(Copy, Clone, Debug, EnumIter)] +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] pub enum Relation {} -impl RelationTrait for Relation { - fn def(&self) -> RelationDef { - panic!("No RelationDef") - } -} - impl ActiveModelBehavior for ActiveModel {}