From 8982d887747a103c4425bb8c541dc22f850dcf77 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Sat, 17 Dec 2022 12:52:04 +0800 Subject: [PATCH] Skip implementing `Related` if the same related entity is being referenced by a conjunct relation (#1298) --- sea-orm-codegen/src/entity/base_entity.rs | 2 ++ sea-orm-codegen/src/entity/relation.rs | 5 +++++ sea-orm-codegen/src/entity/transformer.rs | 10 ++++++++++ sea-orm-codegen/src/entity/writer.rs | 15 ++++++++++++++- 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/sea-orm-codegen/src/entity/base_entity.rs b/sea-orm-codegen/src/entity/base_entity.rs index 85d19aa56..628ef638f 100644 --- a/sea-orm-codegen/src/entity/base_entity.rs +++ b/sea-orm-codegen/src/entity/base_entity.rs @@ -204,6 +204,7 @@ mod tests { on_update: Some(ForeignKeyAction::Cascade), self_referencing: false, num_suffix: 0, + impl_related: true, }, Relation { ref_table: "filling".to_owned(), @@ -214,6 +215,7 @@ mod tests { on_update: Some(ForeignKeyAction::Cascade), self_referencing: false, num_suffix: 0, + impl_related: true, }, ], conjunct_relations: vec![], diff --git a/sea-orm-codegen/src/entity/relation.rs b/sea-orm-codegen/src/entity/relation.rs index 2c29eef9b..ce7b9d821 100644 --- a/sea-orm-codegen/src/entity/relation.rs +++ b/sea-orm-codegen/src/entity/relation.rs @@ -23,6 +23,7 @@ pub struct Relation { pub(crate) on_delete: Option, pub(crate) self_referencing: bool, pub(crate) num_suffix: usize, + pub(crate) impl_related: bool, } impl Relation { @@ -178,6 +179,7 @@ impl From<&TableForeignKey> for Relation { on_update, self_referencing: false, num_suffix: 0, + impl_related: true, } } } @@ -199,6 +201,7 @@ mod tests { on_update: None, self_referencing: false, num_suffix: 0, + impl_related: true, }, Relation { ref_table: "filling".to_owned(), @@ -209,6 +212,7 @@ mod tests { on_update: Some(ForeignKeyAction::Cascade), self_referencing: false, num_suffix: 0, + impl_related: true, }, Relation { ref_table: "filling".to_owned(), @@ -219,6 +223,7 @@ mod tests { on_update: None, self_referencing: false, num_suffix: 0, + impl_related: true, }, ] } diff --git a/sea-orm-codegen/src/entity/transformer.rs b/sea-orm-codegen/src/entity/transformer.rs index 31a4c2a1c..53006cbe0 100644 --- a/sea-orm-codegen/src/entity/transformer.rs +++ b/sea-orm-codegen/src/entity/transformer.rs @@ -197,6 +197,16 @@ impl EntityTransformer { } for (tbl_name, mut conjunct_relations) in conjunct_relations.into_iter() { if let Some(entity) = entities.get_mut(&tbl_name) { + for relation in entity.relations.iter_mut() { + // Skip `impl Related ... { fn to() ... }` implementation block, + // if the same related entity is being referenced by a conjunct relation + if conjunct_relations + .iter() + .any(|conjunct_relation| conjunct_relation.to == relation.ref_table) + { + relation.impl_related = false; + } + } entity.conjunct_relations.append(&mut conjunct_relations); } } diff --git a/sea-orm-codegen/src/entity/writer.rs b/sea-orm-codegen/src/entity/writer.rs index 388e2e4a5..6f293fc7d 100644 --- a/sea-orm-codegen/src/entity/writer.rs +++ b/sea-orm-codegen/src/entity/writer.rs @@ -490,7 +490,7 @@ impl EntityWriter { entity .relations .iter() - .filter(|rel| !rel.self_referencing && rel.num_suffix == 0) + .filter(|rel| !rel.self_referencing && rel.num_suffix == 0 && rel.impl_related) .map(|rel| { let enum_name = rel.get_enum_name(); let module_name = rel.get_module_name(); @@ -707,6 +707,7 @@ mod tests { on_update: None, self_referencing: false, num_suffix: 0, + impl_related: true, }], conjunct_relations: vec![ConjunctRelation { via: "cake_filling".to_owned(), @@ -744,6 +745,7 @@ mod tests { on_update: Some(ForeignKeyAction::Cascade), self_referencing: false, num_suffix: 0, + impl_related: true, }, Relation { ref_table: "filling".to_owned(), @@ -754,6 +756,7 @@ mod tests { on_update: Some(ForeignKeyAction::Cascade), self_referencing: false, num_suffix: 0, + impl_related: true, }, ], conjunct_relations: vec![], @@ -828,6 +831,7 @@ mod tests { on_update: None, self_referencing: false, num_suffix: 0, + impl_related: true, }, Relation { ref_table: "vendor".to_owned(), @@ -838,6 +842,7 @@ mod tests { on_update: None, self_referencing: false, num_suffix: 0, + impl_related: true, }, ], conjunct_relations: vec![], @@ -879,6 +884,7 @@ mod tests { on_update: None, self_referencing: false, num_suffix: 0, + impl_related: true, }], conjunct_relations: vec![], primary_keys: vec![PrimaryKey { @@ -990,6 +996,7 @@ mod tests { on_update: None, self_referencing: true, num_suffix: 1, + impl_related: true, }, Relation { ref_table: "rust_keyword".to_owned(), @@ -1000,6 +1007,7 @@ mod tests { on_update: None, self_referencing: true, num_suffix: 2, + impl_related: true, }, Relation { ref_table: "fruit".to_owned(), @@ -1010,6 +1018,7 @@ mod tests { on_update: None, self_referencing: false, num_suffix: 1, + impl_related: true, }, Relation { ref_table: "fruit".to_owned(), @@ -1020,6 +1029,7 @@ mod tests { on_update: None, self_referencing: false, num_suffix: 2, + impl_related: true, }, Relation { ref_table: "cake".to_owned(), @@ -1030,6 +1040,7 @@ mod tests { on_update: None, self_referencing: false, num_suffix: 0, + impl_related: true, }, ], conjunct_relations: vec![], @@ -1071,6 +1082,7 @@ mod tests { on_update: None, self_referencing: false, num_suffix: 0, + impl_related: true, }], conjunct_relations: vec![ConjunctRelation { via: "cake_filling".to_owned(), @@ -1114,6 +1126,7 @@ mod tests { on_update: None, self_referencing: false, num_suffix: 0, + impl_related: true, }], conjunct_relations: vec![ConjunctRelation { via: "cake_filling".to_owned(),