From dfc4194162292709e929f01f1f0f8161515fb58f Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Tue, 17 Dec 2013 18:54:30 +0800 Subject: [PATCH] Better compatibility for Updates --- do.go | 17 ++++++++++++++--- gorm_test.go | 4 +++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/do.go b/do.go index 8337dc22..1a695915 100644 --- a/do.go +++ b/do.go @@ -5,7 +5,9 @@ import ( "database/sql/driver" "errors" "fmt" + "github.com/jinzhu/gorm/dialect" + "reflect" "regexp" "strconv" @@ -222,9 +224,18 @@ func (s *Do) convertToMapInterface(values interface{}) map[string]interface{} { } } case interface{}: - m := &Model{data: values, do: s} - for _, field := range m.columnsHasValue("other") { - attrs[field.dbName] = field.Value + reflect_value := reflect.ValueOf(values) + + switch reflect_value.Kind() { + case reflect.Map: + for _, key := range reflect_value.MapKeys() { + attrs[toSnake(key.Interface().(string))] = reflect_value.MapIndex(key).Interface() + } + default: + m := &Model{data: values, do: s} + for _, field := range m.columnsHasValue("other") { + attrs[field.dbName] = field.Value + } } } return attrs diff --git a/gorm_test.go b/gorm_test.go index 1bc7c682..31e0480d 100644 --- a/gorm_test.go +++ b/gorm_test.go @@ -5,9 +5,11 @@ import ( "database/sql/driver" "errors" "fmt" + _ "github.com/go-sql-driver/mysql" _ "github.com/lib/pq" _ "github.com/mattn/go-sqlite3" + "os" "reflect" "strconv" @@ -937,7 +939,7 @@ func TestUpdates(t *testing.T) { t.Errorf("Product cde should be renamed to edf") } - db.Table("products").Where("code in (?)", []string{"abc"}).Updates(map[string]interface{}{"code": "fgh", "price": 200}) + db.Table("products").Where("code in (?)", []string{"abc"}).Updates(map[string]string{"code": "fgh", "price": "200"}) if db.First(&Product{}, "code = 'abc'").Error == nil { t.Errorf("Product abc's code should be changed to fgh") }