Better compatibility for Updates

This commit is contained in:
Jinzhu 2013-12-17 18:54:30 +08:00
parent ab67ec5a4e
commit dfc4194162
2 changed files with 17 additions and 4 deletions

17
do.go
View File

@ -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

View File

@ -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")
}