Skip to content

Commit

Permalink
Merge pull request #1046 from bgertsberg11/fix-has-many-with-multiple…
Browse files Browse the repository at this point in the history
…-columns

Fixed has many relationship with multiple columns
  • Loading branch information
j2gg0s authored Nov 4, 2024
2 parents 1bb5597 + 1664b2c commit 1b62093
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions model_table_has_many.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,16 @@ func (m *hasManyModel) ScanRows(ctx context.Context, rows *sql.Rows) (int, error
dest := makeDest(m, len(columns))

var n int

m.structKey = make([]interface{}, len(m.rel.JoinPKs))
for rows.Next() {
if m.sliceOfPtr {
m.strct = reflect.New(m.table.Type).Elem()
} else {
m.strct.Set(m.table.ZeroValue)
}
m.structInited = false

m.scanIndex = 0
m.structKey = m.structKey[:0]

if err := rows.Scan(dest...); err != nil {
return 0, err
}
Expand Down Expand Up @@ -92,9 +91,9 @@ func (m *hasManyModel) Scan(src interface{}) error {
return err
}

for _, f := range m.rel.JoinPKs {
if f.Name == field.Name {
m.structKey = append(m.structKey, indirectFieldValue(field.Value(m.strct)))
for i, f := range m.rel.JoinPKs {
if f.Name == column {
m.structKey[i] = indirectFieldValue(field.Value(m.strct))
break
}
}
Expand Down

0 comments on commit 1b62093

Please sign in to comment.