pgx icon indicating copy to clipboard operation
pgx copied to clipboard

encoding custom types as jsonb no longe works with v5.6 and above

Open grzn opened this issue 1 year ago • 4 comments

Hi @jackc , we use call RegisterDefaultPgType for custom structs like this

RegisterDefaultPgType(SomeStruct, "jsonb")

and everything would work in v5.5.4. Now with v5.7 (and I guess with v5.6 too), we get the following errors on insert:

failed to create outpost: error writing to DB: ERROR: invalid input syntax for type json

not sure if this is the root cause, but I tried to debug it to understand a bit better what's going on and I think this is the reason it chooses the json codec instead of the jsonb codec.

With v5.7.1 is there a new way to define custom types? btw we use QueryExecModeExec.

grzn avatar Sep 19 '24 17:09 grzn

+1

sagitsofan avatar Sep 19 '24 17:09 sagitsofan

Can you provide an example that demonstrates this error?

jackc avatar Sep 21 '24 13:09 jackc

Something like this

type Outpost struct {
...
	DestroyStatuses            OutpostRegionStatuses
}

type OutpostExtendedStatus struct {
...
	Errors             []string                            `json:",omitempty"`
}

type NullableOutpostExtendedStatus struct {
	Val   *OutpostExtendedStatus
	Valid bool
}

then the following code

err := db.Create(&outpost).Error // db is *gorm.DB

returns the following error

invalid input syntax for type json (SQLSTATE 22P02)

we have an AfterConnect hook that does:

conn.TypeMap().RegisterDefaultPgType(&NullableOutpostExtendedStatus{}, "jsonb")

grzn avatar Sep 21 '24 16:09 grzn

I'd need an example I can run.

Perhaps that's what you intended for #2127? It seems to work on master...

jackc avatar Sep 24 '24 22:09 jackc