Make sure UpdateColumn works with CamelCase

This commit is contained in:
Jinzhu 2013-11-24 09:17:13 +08:00
parent 31c64a9c95
commit 7a789c82df
3 changed files with 12 additions and 1 deletions

4
do.go
View File

@ -208,7 +208,9 @@ func (s *Do) convertToMapInterface(values interface{}) map[string]interface{} {
switch value := values.(type) { switch value := values.(type) {
case map[string]interface{}: case map[string]interface{}:
attrs = value for k, v := range value {
attrs[toSnake(k)] = v
}
case []interface{}: case []interface{}:
for _, v := range value { for _, v := range value {
for key, value := range s.convertToMapInterface(v) { for key, value := range s.convertToMapInterface(v) {

View File

@ -868,6 +868,14 @@ func TestUpdate(t *testing.T) {
if db.First(&Product{}, "code = '789'").Error != nil { if db.First(&Product{}, "code = '789'").Error != nil {
t.Errorf("Product 456 should be changed to 789") t.Errorf("Product 456 should be changed to 789")
} }
if db.Model(&product2).Update("CreatedAt", time.Now().Add(time.Hour)).Error != nil {
t.Error("No error should raise when update with CamelCase")
}
if db.Model(&product2).UpdateColumn("CreatedAt", time.Now().Add(time.Hour)).Error != nil {
t.Error("No error should raise when update_column with CamelCase")
}
} }
func TestUpdates(t *testing.T) { func TestUpdates(t *testing.T) {

View File

@ -132,6 +132,7 @@ func (m *Model) updatedColumnsAndValues(values map[string]interface{}, ignore_pr
data := m.reflectData() data := m.reflectData()
if !data.CanAddr() { if !data.CanAddr() {
m.do.err(errors.New("Can't take address of the object"))
return return
} }