pgx
pgx copied to clipboard
encoding custom types as jsonb no longe works with v5.6 and above
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.
+1
Can you provide an example that demonstrates this error?
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")
I'd need an example I can run.
Perhaps that's what you intended for #2127? It seems to work on master...