From 48e41440afa6a741a3e345f2cfbabca08f6fb1ac Mon Sep 17 00:00:00 2001 From: Adrian Heng Date: Fri, 9 Feb 2018 08:22:30 +0800 Subject: [PATCH] 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 --- dialect_postgres.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/dialect_postgres.go b/dialect_postgres.go index 6fdf4df1..3bcea536 100644 --- a/dialect_postgres.go +++ b/dialect_postgres.go @@ -1,6 +1,7 @@ package gorm import ( + "encoding/json" "fmt" "reflect" "strings" @@ -68,9 +69,14 @@ func (s *postgres) DataTypeOf(field *StructField) string { default: if IsByteArrayOrSlice(dataValue) { sqlType = "bytea" + if isUUID(dataValue) { sqlType = "uuid" } + + if isJSON(dataValue) { + sqlType = "jsonb" + } } } } @@ -130,3 +136,8 @@ func isUUID(value reflect.Value) bool { lower := strings.ToLower(typename) return "uuid" == lower || "guid" == lower } + +func isJSON(value reflect.Value) bool { + _, ok := value.Interface().(json.RawMessage) + return ok +}