Skip to content

Commit

Permalink
Remove duplicate relationships in many-to-many self joins
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenafamo committed Oct 14, 2023
1 parent 7ab7b9f commit df3d920
Showing 1 changed file with 4 additions and 19 deletions.
23 changes: 4 additions & 19 deletions gen/drivers/relationships.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit df3d920

Please sign in to comment.