Sort column names before generating SQL in `DB.UpdateColumns` (#1734)

This commit is contained in:
Jess Smith 2018-02-10 01:26:01 -05:00 committed by Jinzhu
parent 21fb3ae1fe
commit aa3fd6de13
1 changed files with 11 additions and 1 deletions

View File

@ -3,6 +3,7 @@ package gorm
import ( import (
"errors" "errors"
"fmt" "fmt"
"sort"
"strings" "strings"
) )
@ -59,7 +60,16 @@ func updateCallback(scope *Scope) {
var sqls []string var sqls []string
if updateAttrs, ok := scope.InstanceGet("gorm:update_attrs"); ok { if updateAttrs, ok := scope.InstanceGet("gorm:update_attrs"); ok {
for column, value := range updateAttrs.(map[string]interface{}) { // Sort the column names so that the generated SQL is the same every time.
updateMap := updateAttrs.(map[string]interface{})
var columns []string
for c := range updateMap {
columns = append(columns, c)
}
sort.Strings(columns)
for _, column := range columns {
value := updateMap[column]
sqls = append(sqls, fmt.Sprintf("%v = %v", scope.Quote(column), scope.AddToVars(value))) sqls = append(sqls, fmt.Sprintf("%v = %v", scope.Quote(column), scope.AddToVars(value)))
} }
} else { } else {