pgtype icon indicating copy to clipboard operation
pgtype copied to clipboard

comparing uncomparable type pgtype.VarcharArray

Open bweston92 opened this issue 1 year ago • 3 comments

To Reproduce

Use a Postgres type varchar[]

Create a query that queries it:

select mytype from mytype;

You'll see something like this in the generated code

// ...

type MyType struct {
	ID                *int32              `json:"id"`
	RemoteIdentifiers pgtype.VarcharArray `json:"remote_identifiers"`
}

// ...
func (tr *typeResolver) newMyType() pgtype.ValueTranscoder {
	return tr.newCompositeValue(
		"mytype",
		compositeField{name: "id", typeName: "int4", defaultVal: &pgtype.Int4{}},
// ...
		compositeField{name: "remote_identifiers", typeName: "_varchar", defaultVal: &pgtype.VarcharArray{}},
// ...
	)
}

Expected behavior

res, err := queries.FindMyType(ctx, 1)
// ...
var ids []string
_ = res.RemoteIdentifiers.AssignTo(&ids)
fmt.Printf("%+v", ids)
// to output a slice of string

Actual behavior

panic: runtime error: comparing uncomparable type pgtype.VarcharArray

goroutine 1 [running]:
github.com/jackc/pgtype.(*VarcharArray).Set(0xc0004e0038, {0xd200a0, 0xc0003f0180})
        external/com_github_jackc_pgtype/varchar_array.go:29 +0x114
github.com/jackc/pgtype.assignToOrSet({0xeaffc0, 0xc0003f0140}, {0xd2b740, 0xc0004e0038})
        external/com_github_jackc_pgtype/composite_type.go:171 +0xa2
github.com/jackc/pgtype.CompositeType.assignToPtrStruct({0x2, {0xd8047e, 0x6}, {0xc00005c200, 0x5, 0x5}, {0xc00051e410, 0x5, 0x5}}, {0xbf5c80, ...})
        external/com_github_jackc_pgtype/composite_type.go:212 +0x3d7
github.com/jackc/pgtype.CompositeType.AssignTo({0x2, {0xd8047e, 0x6}, {0xc00005c200, 0x5, 0x5}, {0xc00051e410, 0x5, 0x5}}, {0xbf5c80, ...})
        external/com_github_jackc_pgtype/composite_type.go:150 +0x1fb

bweston92 avatar Jun 10 '24 18:06 bweston92

Error goes away and runs fine if I remove these lines https://github.com/jackc/pgtype/blob/a4d4bbf043f7988ea29696a612cf311026fedf92/varchar_array.go#L27-L32

bweston92 avatar Jun 10 '24 18:06 bweston92

There's nothing that someone else can try to run here.

You'll see something like this in the generated code

What do you mean generated code? pgtype doesn't have any code that looks like that.


Also, I'd really suggest using pgx v5 instead of v4.

jackc avatar Jun 10 '24 23:06 jackc

What do you mean generated code? pgtype doesn't have any code that looks like that.

Sorry I originally wrote this in the context of pgx until I realised the error was in this package.

I will try to get a failing test today if I can reproduce easily.

bweston92 avatar Jun 11 '24 06:06 bweston92