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})
This commit is contained in:
qAison 2018-10-12 16:14:39 +08:00
parent 742154be9a
commit fa104e4300

View File

@ -905,7 +905,14 @@ func convertInterfaceToMap(values interface{}, withIgnoredField bool) map[string
func (scope *Scope) updatedAttrsWithValues(value interface{}) (results map[string]interface{}, hasUpdate bool) { func (scope *Scope) updatedAttrsWithValues(value interface{}) (results map[string]interface{}, hasUpdate bool) {
if scope.IndirectValue().Kind() != reflect.Struct { 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{}{} results = map[string]interface{}{}
@ -1054,6 +1061,25 @@ func (scope *Scope) changeableField(field *Field) bool {
return true 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 { func (scope *Scope) related(value interface{}, foreignKeys ...string) *Scope {
toScope := scope.db.NewScope(value) toScope := scope.db.NewScope(value)
tx := scope.db.Set("gorm:association:source", scope.Value) tx := scope.db.Set("gorm:association:source", scope.Value)