entc/gen: filter duplicate ids when loading o2m/m2o edges

This commit is contained in:
Ariel Mashraki
2021-03-15 22:07:15 +02:00
committed by Ariel Mashraki
parent 0f7739d83b
commit 6ab0d01ea4
39 changed files with 286 additions and 163 deletions

View File

@@ -317,16 +317,16 @@ func ({{ $receiver }} *{{ $builder }}) sqlQuery(ctx context.Context) *sql.Select
nodeids := make(map[{{ $e.Type.ID.Type }}][]*{{ $.Name }})
for i := range nodes {
{{- $fk := $e.ForeignKey }}
fk := nodes[i].{{ $fk.StructField }}
{{- if $fk.Field.Nillable }}
if fk != nil {
ids = append(ids, *fk)
nodeids[*fk] = append(nodeids[*fk], nodes[i])
if nodes[i].{{ $fk.StructField }} == nil {
continue
}
{{- else }}
ids = append(ids, fk)
nodeids[fk] = append(nodeids[fk], nodes[i])
{{- end }}
fk := {{ if $fk.Field.Nillable }}*{{ end }}nodes[i].{{ $fk.StructField }}
if _, ok := nodeids[fk]; !ok {
ids = append(ids, fk)
}
nodeids[fk] = append(nodeids[fk], nodes[i])
}
query.Where({{ $e.Type.Package }}.IDIn(ids...))
neighbors, err := query.All(ctx)