Skip to content

Commit

Permalink
Skip implementing Related if the same related entity is being refer…
Browse files Browse the repository at this point in the history
…enced by a conjunct relation (#1298)
  • Loading branch information
billy1624 authored Dec 17, 2022
1 parent 7757a46 commit 8982d88
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
2 changes: 2 additions & 0 deletions sea-orm-codegen/src/entity/base_entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -214,6 +215,7 @@ mod tests {
on_update: Some(ForeignKeyAction::Cascade),
self_referencing: false,
num_suffix: 0,
impl_related: true,
},
],
conjunct_relations: vec![],
Expand Down
5 changes: 5 additions & 0 deletions sea-orm-codegen/src/entity/relation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub struct Relation {
pub(crate) on_delete: Option<ForeignKeyAction>,
pub(crate) self_referencing: bool,
pub(crate) num_suffix: usize,
pub(crate) impl_related: bool,
}

impl Relation {
Expand Down Expand Up @@ -178,6 +179,7 @@ impl From<&TableForeignKey> for Relation {
on_update,
self_referencing: false,
num_suffix: 0,
impl_related: true,
}
}
}
Expand All @@ -199,6 +201,7 @@ mod tests {
on_update: None,
self_referencing: false,
num_suffix: 0,
impl_related: true,
},
Relation {
ref_table: "filling".to_owned(),
Expand All @@ -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(),
Expand All @@ -219,6 +223,7 @@ mod tests {
on_update: None,
self_referencing: false,
num_suffix: 0,
impl_related: true,
},
]
}
Expand Down
10 changes: 10 additions & 0 deletions sea-orm-codegen/src/entity/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
15 changes: 14 additions & 1 deletion sea-orm-codegen/src/entity/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand All @@ -754,6 +756,7 @@ mod tests {
on_update: Some(ForeignKeyAction::Cascade),
self_referencing: false,
num_suffix: 0,
impl_related: true,
},
],
conjunct_relations: vec![],
Expand Down Expand Up @@ -828,6 +831,7 @@ mod tests {
on_update: None,
self_referencing: false,
num_suffix: 0,
impl_related: true,
},
Relation {
ref_table: "vendor".to_owned(),
Expand All @@ -838,6 +842,7 @@ mod tests {
on_update: None,
self_referencing: false,
num_suffix: 0,
impl_related: true,
},
],
conjunct_relations: vec![],
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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(),
Expand All @@ -1000,6 +1007,7 @@ mod tests {
on_update: None,
self_referencing: true,
num_suffix: 2,
impl_related: true,
},
Relation {
ref_table: "fruit".to_owned(),
Expand All @@ -1010,6 +1018,7 @@ mod tests {
on_update: None,
self_referencing: false,
num_suffix: 1,
impl_related: true,
},
Relation {
ref_table: "fruit".to_owned(),
Expand All @@ -1020,6 +1029,7 @@ mod tests {
on_update: None,
self_referencing: false,
num_suffix: 2,
impl_related: true,
},
Relation {
ref_table: "cake".to_owned(),
Expand All @@ -1030,6 +1040,7 @@ mod tests {
on_update: None,
self_referencing: false,
num_suffix: 0,
impl_related: true,
},
],
conjunct_relations: vec![],
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit 8982d88

Please sign in to comment.