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

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

Open
grzn opened this issue Sep 19, 2024 · 4 comments
Open

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

grzn opened this issue Sep 19, 2024 · 4 comments
Labels

Comments

@grzn
Copy link

grzn commented Sep 19, 2024

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 grzn added the bug label Sep 19, 2024
@sagitsofan
Copy link

+1

@jackc
Copy link
Owner

jackc commented Sep 21, 2024

Can you provide an example that demonstrates this error?

@grzn
Copy link
Author

grzn commented Sep 21, 2024

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")

@jackc
Copy link
Owner

jackc commented Sep 24, 2024

I'd need an example I can run.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants