Skip to content

Commit

Permalink
fix(m2m): correctly infer which columns belong to which model
Browse files Browse the repository at this point in the history
The first `len(m.rel.M2MBaseFields)` columns are always coming from the
M2M table, as specified in the M2M join query in function
`relationJoin.m2mQuery()`. Thus we compare the `scanIndex` to this
value.
Additionally, more columns can be requested by the caller, and so the
checking the `m.table.FieldMap` is still relevant.
  • Loading branch information
ygabuev committed Jun 5, 2024
1 parent bcbffba commit 1e623bd
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion model_table_m2m.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,13 @@ func (m *m2mModel) ScanRows(ctx context.Context, rows *sql.Rows) (int, error) {
}

func (m *m2mModel) Scan(src interface{}) error {
defer func() { m.scanIndex++ }()

column := m.columns[m.scanIndex]
m.scanIndex++

if m.scanIndex < len(m.rel.M2MBaseFields) {
return m.scanM2MColumn(column, src)
}

field, ok := m.table.FieldMap[column]
if !ok {
Expand Down

0 comments on commit 1e623bd

Please sign in to comment.