LoadTypes 始终返回空切片
Describe the bug
It appears that LoadTypes always returns an empty slice of pgtype.Types, even if err is nil. Loading the same types individually works. This leads to some errors when using arrays with enum types. To Reproduce Steps to reproduce the behavior: go package main import "context" "fmt" "GitHub.com/jackc/pgx/v5" type Color string type HasColor struct { Color Color db:"color" Colors []Color db:"colors" } func main() { Works() DoesntWork() } func Works() { conn := InitConn() defer conn.Close(context.Background()) InitDB(conn) for _, s := range []string{"COLOR", "_COLOR"} { t, err := conn.LoadType(context.Background(), s) if err != nil { panic(err) } else { fmt.Println(t) } conn.TypeMap().RegisterType(t) } RunTest(conn) } func DoesntWork() { conn := InitConn() defer conn.Close(context.Background()) InitDB(conn) types, err := conn.LoadTypes(context.Background(), []string{"COLOR", "_COLOR"}) if err != nil { panic(err) } else { // types is empty! fmt.Println(types) } conn.TypeMap().RegisterTypes(types) // Doesn't work: can't scan into dest[0]: cannot scan unknown type (OID 16389) in text format into *[]main.Color RunTest(conn) } func InitConn() *pgx.Conn { conn, err := pgx.Connect(context.Background(), "postgres://local:local@localhost:5432/local") if err != nil { panic(err) } return conn } func InitDB(conn *pgx.Conn) { _, err := conn.Exec(context.Background(), CREATE TYPE COLOR AS ENUM ('red', 'green', 'blue'); CREATE TABLE HasColor (id UUID PRIMARY KEY DEFAULT gen_random_uuid(), color COLOR, colors COLOR[]); INSERT INTO HasColor (color, colors) VALUES ('red', '{green}')) if err != nil { fmt.Println(err) } } func RunTest(conn *pgx.Conn) { var result HasColor err := conn.QueryRow(context.Background(), "select color from HasColor").Scan(&result.Color) if err != nil { fmt.Println(err) } else { fmt.Println(result) } err = conn.QueryRow(context.Background(), "select colors from HasColor").Scan(&result.Colors) if err != nil { fmt.Println(err) } else { fmt.Println(result) } }
内容来源: jackc/pgx