mirror of https://github.com/go-gorm/gorm.git
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:
parent
3f98904fe7
commit
48e41440af
|
@ -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
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue