Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sql.NullString mistaken as custom struct #1019

Merged
merged 1 commit into from
Oct 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions dialect/pgdialect/sqltype.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pgdialect

import (
"database/sql"
"encoding/json"
"net"
"reflect"
Expand All @@ -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
)
Expand All @@ -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 {
Expand Down Expand Up @@ -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:
Expand All @@ -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
}
Expand Down
Loading