From fa104e43002904d97ee87d7eb06c6b6fe073a08b Mon Sep 17 00:00:00 2001 From: qAison <77456702@qq.com> Date: Fri, 12 Oct 2018 16:14:39 +0800 Subject: [PATCH] Fix: Select and Omit operations are invalid db.Table("user").Select("name").Updates(User{Name: "hello", Sort: 2}) db.Table("user").Omit("name").Updates(map[string]interface{}{"name": "hello", "sort": 3}) --- scope.go | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/scope.go b/scope.go index 378025bd..a97ba8b3 100644 --- a/scope.go +++ b/scope.go @@ -905,7 +905,14 @@ func convertInterfaceToMap(values interface{}, withIgnoredField bool) map[string func (scope *Scope) updatedAttrsWithValues(value interface{}) (results map[string]interface{}, hasUpdate bool) { if scope.IndirectValue().Kind() != reflect.Struct { - return convertInterfaceToMap(value, false), true + results = convertInterfaceToMap(value, false) + for key, _ := range results { + if !scope.changeableFieldName(key) { + delete(results, key) + } + } + + return results, true } results = map[string]interface{}{} @@ -1054,6 +1061,25 @@ func (scope *Scope) changeableField(field *Field) bool { return true } +func (scope *Scope) changeableFieldName(name string) bool { + if selectAttrs := scope.SelectAttrs(); len(selectAttrs) > 0 { + for _, attr := range selectAttrs { + if name == attr { + return true + } + } + return false + } + + for _, attr := range scope.OmitAttrs() { + if name == attr { + return false + } + } + + return true +} + func (scope *Scope) related(value interface{}, foreignKeys ...string) *Scope { toScope := scope.db.NewScope(value) tx := scope.db.Set("gorm:association:source", scope.Value)