diff --git a/dialect/pgdialect/sqltype.go b/dialect/pgdialect/sqltype.go index 40802e51d..fad84209d 100644 --- a/dialect/pgdialect/sqltype.go +++ b/dialect/pgdialect/sqltype.go @@ -1,6 +1,7 @@ package pgdialect import ( + "database/sql" "encoding/json" "net" "reflect" @@ -27,13 +28,6 @@ const ( pgTypeSerial = "SERIAL" // 4 byte autoincrementing integer pgTypeBigSerial = "BIGSERIAL" // 8 byte autoincrementing integer - // Character Types - pgTypeChar = "CHAR" // fixed length string (blank padded) - pgTypeText = "TEXT" // variable length string without limit - - // JSON Types - pgTypeJSON = "JSON" // text representation of json data - // Binary Data Types pgTypeBytea = "BYTEA" // binary string ) @@ -42,6 +36,7 @@ var ( ipType = reflect.TypeOf((*net.IP)(nil)).Elem() ipNetType = reflect.TypeOf((*net.IPNet)(nil)).Elem() jsonRawMessageType = reflect.TypeOf((*json.RawMessage)(nil)).Elem() + nullStringType = reflect.TypeOf((*sql.NullString)(nil)).Elem() ) func (d *Dialect) DefaultVarcharLen() int { @@ -77,6 +72,8 @@ func fieldSQLType(field *schema.Field) string { func sqlType(typ reflect.Type) string { switch typ { + case nullStringType: // typ.Kind() == reflect.Struct, test for exact match + return sqltype.VarChar case ipType: return pgTypeInet case ipNetType: @@ -92,7 +89,7 @@ func sqlType(typ reflect.Type) string { } switch typ.Kind() { - case reflect.Map, reflect.Struct: + case reflect.Map, reflect.Struct: // except typ == nullStringType, see above if sqlType == sqltype.VarChar { return sqltype.JSONB }