Makes order of values deterministic for IN(?, ?..) style query

Currently during preloads of associations the order of parameterized/comma-
separated values for IN(?, ?..) style query is not deterministic. This commit
fixes that which removes random failure when writing tests which asserts exact
queries run (e.g. using DATA-DOG/go-sqlmock library or such).
This commit is contained in:
Jitendra Ojha 2020-02-12 13:37:49 +05:30
parent 7180bd0f27
commit 20cb84ca8d

View File

@ -1324,7 +1324,7 @@ func (scope *Scope) autoIndex() *Scope {
}
func (scope *Scope) getColumnAsArray(columns []string, values ...interface{}) (results [][]interface{}) {
resultMap := make(map[string][]interface{})
resultMap := make(map[string]bool)
for _, value := range values {
indirectValue := indirect(reflect.ValueOf(value))
@ -1345,7 +1345,8 @@ func (scope *Scope) getColumnAsArray(columns []string, values ...interface{}) (r
if hasValue {
h := fmt.Sprint(result...)
if _, exist := resultMap[h]; !exist {
resultMap[h] = result
resultMap[h] = true
results = append(results, result)
}
}
}
@ -1363,14 +1364,12 @@ func (scope *Scope) getColumnAsArray(columns []string, values ...interface{}) (r
if hasValue {
h := fmt.Sprint(result...)
if _, exist := resultMap[h]; !exist {
resultMap[h] = result
resultMap[h] = true
results = append(results, result)
}
}
}
}
for _, v := range resultMap {
results = append(results, v)
}
return
}