From df3d920cf6cc946fcdf8da238edf95be3110b0cc Mon Sep 17 00:00:00 2001 From: Stephen Afam-Osemene Date: Sat, 14 Oct 2023 08:15:32 +0100 Subject: [PATCH] Remove duplicate relationships in many-to-many self joins --- gen/drivers/relationships.go | 23 ++++------------------- 1 file changed, 4 insertions(+), 19 deletions(-) diff --git a/gen/drivers/relationships.go b/gen/drivers/relationships.go index efbc66fd..d1c5eecc 100644 --- a/gen/drivers/relationships.go +++ b/gen/drivers/relationships.go @@ -83,25 +83,6 @@ func BuildRelationships(tables []Table) map[string][]orm.Relationship { }}, }) } - - // Adding the inverse relationship - // if !t1.IsJoinTable && t1.Key != t2.Key { - // if t1.Name == "users" { - // fmt.Println("ADDING") - // } - // relationships[t2.Key] = append(relationships[t2.Key], orm.Relationship{ - // Name: fk.Name, - // Sides: []orm.RelSide{{ - // From: t2.Key, - // FromColumns: fk.ForeignColumns, - // To: t1.Key, - // ToColumns: fk.Columns, - // ToKey: true, - // ToUnique: localUnique, - // KeyNullable: localNullable, - // }}, - // }) - // } } if !t1.IsJoinTable { @@ -139,6 +120,10 @@ func BuildRelationships(tables []Table) map[string][]orm.Relationship { }, }, }) + // It is a many-to-many self join no need to duplicate the relationship + if r1.Sides[0].To == r2.Sides[0].To { + continue + } relationships[r2.Sides[0].To] = append(relationships[r2.Sides[0].To], orm.Relationship{ Name: r1.Name + r2.Name, ByJoinTable: true,