From 1664b2c07a5f6cfd3b6730e5005373686e9830a6 Mon Sep 17 00:00:00 2001 From: Boris Gertsberg Date: Thu, 31 Oct 2024 09:50:16 -0400 Subject: [PATCH] fix: has many relationship with multiple columns --- model_table_has_many.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/model_table_has_many.go b/model_table_has_many.go index 544cdf5d6..cd721a1b2 100644 --- a/model_table_has_many.go +++ b/model_table_has_many.go @@ -51,7 +51,7 @@ 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() @@ -59,9 +59,8 @@ func (m *hasManyModel) ScanRows(ctx context.Context, rows *sql.Rows) (int, error 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 } @@ -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 } }