From 20cb84ca8df2bbc36be16ceb91c4b99eb8124c04 Mon Sep 17 00:00:00 2001 From: Jitendra Ojha Date: Wed, 12 Feb 2020 13:37:49 +0530 Subject: [PATCH] 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). --- scope.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/scope.go b/scope.go index d82cadbc..21d98ad6 100644 --- a/scope.go +++ b/scope.go @@ -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 }