forked from mirror/gorm
Better compatibility for Updates
This commit is contained in:
parent
ab67ec5a4e
commit
dfc4194162
17
do.go
17
do.go
|
@ -5,7 +5,9 @@ import (
|
||||||
"database/sql/driver"
|
"database/sql/driver"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/jinzhu/gorm/dialect"
|
"github.com/jinzhu/gorm/dialect"
|
||||||
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -222,9 +224,18 @@ func (s *Do) convertToMapInterface(values interface{}) map[string]interface{} {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case interface{}:
|
case interface{}:
|
||||||
m := &Model{data: values, do: s}
|
reflect_value := reflect.ValueOf(values)
|
||||||
for _, field := range m.columnsHasValue("other") {
|
|
||||||
attrs[field.dbName] = field.Value
|
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
|
return attrs
|
||||||
|
|
|
@ -5,9 +5,11 @@ import (
|
||||||
"database/sql/driver"
|
"database/sql/driver"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
_ "github.com/lib/pq"
|
_ "github.com/lib/pq"
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
|
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -937,7 +939,7 @@ func TestUpdates(t *testing.T) {
|
||||||
t.Errorf("Product cde should be renamed to edf")
|
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 {
|
if db.First(&Product{}, "code = 'abc'").Error == nil {
|
||||||
t.Errorf("Product abc's code should be changed to fgh")
|
t.Errorf("Product abc's code should be changed to fgh")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue