From 49fa9c7f4a93e96a0542bec4fb762db16090ac4f Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Sat, 30 Nov 2013 19:56:20 +0800 Subject: [PATCH] Convert string to int automatically when query and update --- model.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/model.go b/model.go index 17517dca..c21a6bae 100644 --- a/model.go +++ b/model.go @@ -5,6 +5,7 @@ import ( "go/ast" "reflect" "regexp" + "strconv" "time" ) @@ -141,6 +142,13 @@ func (m *Model) updatedColumnsAndValues(values map[string]interface{}, ignore_pr if field.Interface() != value { switch field.Kind() { case reflect.Int, reflect.Int32, reflect.Int64: + if s, ok := value.(string); ok { + i, err := strconv.Atoi(s) + if m.do.err(err) == nil { + value = i + } + } + if field.Int() != reflect.ValueOf(value).Int() { any_updated = true field.SetInt(reflect.ValueOf(value).Int())