Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues with many-to-many relationship loading #139

Merged
merged 2 commits into from
Jan 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 37 additions & 12 deletions gen/templates/models/10_rel_load.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,16 @@ func (os {{$tAlias.UpSingular}}Slice) Load{{$tAlias.UpSingular}}{{$relAlias}}(ct
if len(os) == 0 {
return nil
}

// since we are changing the columns, we need to check if the original columns were set or add the defaults
sq := dialect.SelectQuery{}
for _, mod := range mods {
mod.Apply(&sq)
}

if len(sq.SelectList.Columns) == 0 {
mods = append(mods, sm.Columns({{$fAlias.UpPlural}}.Columns()))
}

q := os.{{relQueryMethodName $tAlias $relAlias}}(ctx, exec, append(
mods,
Expand All @@ -270,15 +280,30 @@ func (os {{$tAlias.UpSingular}}Slice) Load{{$tAlias.UpSingular}}{{$relAlias}}(ct
{{- end}}
)...)

{{range $index, $local := $firstSide.FromColumns -}}
{{- $fromColAlias := index $firstFrom.Columns $local -}}
{{- $fromCol := getColumn $.Tables $firstSide.From $firstFrom $local -}}
{{$fromColAlias}}Slice := []{{$fromCol.Type}}{}
{{end}}

{{$.Importer.Import "github.com/stephenafamo/scan" -}}
{{$fAlias.DownPlural}}, err := bob.All(ctx, exec, q, scan.StructMapper[*struct{
{{$fAlias.UpSingular}}
{{range $index, $local := $firstSide.FromColumns -}}
{{- $fromColAlias := index $firstFrom.Columns $local -}}
{{- $fromCol := getColumn $.Tables $firstSide.From $firstFrom $local -}}
Related{{$fromColAlias}} {{$fromCol.Type}} `db:"related_{{$firstSide.From}}.{{$fromColAlias}}"`
{{end}}
}]())
mapper := scan.Mod(scan.StructMapper[*{{$fAlias.UpSingular}}](), func(ctx context.Context, cols []string) (scan.BeforeFunc, func(any, any) error) {
return func(row *scan.Row) (any, error) {
{{range $index, $local := $firstSide.FromColumns -}}
{{- $fromColAlias := index $firstFrom.Columns $local -}}
{{- $fromCol := getColumn $.Tables $firstSide.From $firstFrom $local -}}
{{$fromColAlias}}Slice = append({{$fromColAlias}}Slice, *new({{$fromCol.Type}}))
row.ScheduleScan("related_{{$firstSide.From}}.{{$fromColAlias}}", &{{$fromColAlias}}Slice[len({{$fromColAlias}}Slice)-1])
{{end}}

return nil, nil
},
func(any, any) error {
return nil
}
})

{{$fAlias.DownPlural}}, err := bob.Allx[*{{$fAlias.UpSingular}}, {{$fAlias.UpSingular}}Slice](ctx, exec, q, mapper)
if err != nil {
return err
}
Expand All @@ -290,10 +315,10 @@ func (os {{$tAlias.UpSingular}}Slice) Load{{$tAlias.UpSingular}}{{$relAlias}}(ct
{{- end}}

for _, o := range os {
for _, rel := range {{$fAlias.DownPlural}} {
for i, rel := range {{$fAlias.DownPlural}} {
{{range $index, $local := $firstSide.FromColumns -}}
{{$fromCol := index $firstFrom.Columns $local -}}
if o.{{$fromCol}} != rel.Related{{$fromCol}} {
if o.{{$fromCol}} != {{$fromCol}}Slice[i] {
continue
}
{{end}}
Expand All @@ -309,9 +334,9 @@ func (os {{$tAlias.UpSingular}}Slice) Load{{$tAlias.UpSingular}}{{$relAlias}}(ct


{{if $rel.IsToMany -}}
o.R.{{$relAlias}} = append(o.R.{{$relAlias}}, &rel.{{$fAlias.UpSingular}})
o.R.{{$relAlias}} = append(o.R.{{$relAlias}}, rel)
{{else -}}
o.R.{{$relAlias}} = &rel.{{$fAlias.UpSingular}}
o.R.{{$relAlias}} = rel
break
{{end -}}
}
Expand Down