Skip to content

Commit

Permalink
Sort column names before generating SQL in DB.UpdateColumns (go-gor…
Browse files Browse the repository at this point in the history
  • Loading branch information
smithjessk authored and jinzhu committed Feb 10, 2018
1 parent 21fb3ae commit aa3fd6d
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion callback_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gorm
import (
"errors"
"fmt"
"sort"
"strings"
)

Expand Down Expand Up @@ -59,7 +60,16 @@ func updateCallback(scope *Scope) {
var sqls []string

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)))
}
} else {
Expand Down

0 comments on commit aa3fd6d

Please sign in to comment.