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

stdlib: scan jsonb column into *json.RawMessage #409

Closed
jbowens opened this issue Apr 11, 2018 · 2 comments
Closed

stdlib: scan jsonb column into *json.RawMessage #409

jbowens opened this issue Apr 11, 2018 · 2 comments

Comments

@jbowens
Copy link
Contributor

jbowens commented Apr 11, 2018

I'm experimenting with converting a codebase from lib/pq to pgx. There are many instances where we Scan a jsonb value into a *json.RawMessage type. With lib/pq, this works automatically, I assume because lib/pq surfaces jsonb values to database/sql as a []byte. Using pgx, I get

sql: Scan error on column index 3: unsupported Scan, storing driver.Value type string into type *json.RawMessage

seeming to indicate pgx/stdlib surfaces the jsonb value to database/sql as a string. Is string the appropriate type? Sorry if this is an uneducated question, as I'm fairly ignorant of the innards of database/sql.

It would be really convenient to be able to Scan directly into json.RawMessage types. I can always work around this by scanning into a []byte and converting to a json.RawMessage later.

@jackc jackc closed this as completed in 5297846 Apr 14, 2018
@jackc
Copy link
Owner

jackc commented Apr 14, 2018

It's hard to say whether string or []byte is the true / better representation of PostgreSQL json text. But I was able to change it to []byte. database/sql will automatically convert []byte to string when scanning so this change enables your use case (and becomes compatible with lib/pq) without any backwards incompatibility.

@jbowens
Copy link
Contributor Author

jbowens commented Apr 16, 2018

Thanks for implementing this and the quick response!

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

No branches or pull requests

2 participants