Allow for proper table creation with Jsonb fields (#1758)

* DataTypeOf should now correctly identify dataValues that are 'json.RawMessage' types as 'jsonb' columns

* move the json check to its own function

* ran gofmt and did some minor tweaks to satisfy CodeClimate
This commit is contained in:
Adrian Heng 2018-02-09 08:22:30 +08:00 committed by Jinzhu
parent 3f98904fe7
commit 48e41440af
1 changed files with 11 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package gorm package gorm
import ( import (
"encoding/json"
"fmt" "fmt"
"reflect" "reflect"
"strings" "strings"
@ -68,9 +69,14 @@ func (s *postgres) DataTypeOf(field *StructField) string {
default: default:
if IsByteArrayOrSlice(dataValue) { if IsByteArrayOrSlice(dataValue) {
sqlType = "bytea" sqlType = "bytea"
if isUUID(dataValue) { if isUUID(dataValue) {
sqlType = "uuid" sqlType = "uuid"
} }
if isJSON(dataValue) {
sqlType = "jsonb"
}
} }
} }
} }
@ -130,3 +136,8 @@ func isUUID(value reflect.Value) bool {
lower := strings.ToLower(typename) lower := strings.ToLower(typename)
return "uuid" == lower || "guid" == lower return "uuid" == lower || "guid" == lower
} }
func isJSON(value reflect.Value) bool {
_, ok := value.Interface().(json.RawMessage)
return ok
}