From 87c77b8911f2035b0ee8ea96356a2c7600b5b94d Mon Sep 17 00:00:00 2001 From: "Mike L." Date: Sat, 26 Oct 2024 08:23:53 +0200 Subject: [PATCH] fix: sql.NullString mistaken as custom struct (#1019) --- dialect/pgdialect/sqltype.go | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) 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 }