mirror of https://github.com/go-gorm/gorm.git
Sort column names before generating SQL in `DB.UpdateColumns` (#1734)
This commit is contained in:
parent
21fb3ae1fe
commit
aa3fd6de13
|
@ -3,6 +3,7 @@ package gorm
|
|||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue