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

Document how to use Json<T> with the query macros #3153

Open
ElysaSrc opened this issue Mar 25, 2024 · 2 comments
Open

Document how to use Json<T> with the query macros #3153

ElysaSrc opened this issue Mar 25, 2024 · 2 comments
Labels
documentation Improvements or additions to documentation macros

Comments

@ElysaSrc
Copy link

Bug Description

The Json support for postgresql only works through the usage of the Value type. The Json type when used as show in the documentation doesn't work : https://docs.rs/sqlx/latest/sqlx/types/struct.Json.html

I've checked the source and I have the feeling that the Json module is not covered by tests, it may have been broken in one refactoring.

Minimal Reproduction

I've made a minimal repo that repro the compiler error.

https://github.com/ElysaSrc/repro-sqlx-json-bug

Info

  • SQLx version: 0.7.4
  • SQLx features enabled: ["runtime-tokio","tls-rustls","postgres","chrono","migrate","uuid","json"]
  • Database server and version: Postgres 14
  • Operating system: Nixos unstable - Linux 6.7.9
  • rustc --version: rustc 1.77.0 (aedd173a2 2024-03-17)
@ElysaSrc ElysaSrc added the bug label Mar 25, 2024
@abonander
Copy link
Collaborator

Not a bug. The query macros map JSON columns to serde_json::Value by default. You have to tell the macro to use the Json adapter using the type override syntax: https://docs.rs/sqlx/latest/sqlx/macro.query.html#force-a-differentcustom-type

See this example (uses query!() but the concept is the same): https://github.com/launchbadge/sqlx/blob/main/examples/postgres/json/src/main.rs#L80

Note also that query_as!() does not currently use FromRow as stated in the docs: https://docs.rs/sqlx/latest/sqlx/macro.query_as.html

Leaving this open as a documentation issue.

@abonander abonander added documentation Improvements or additions to documentation macros and removed bug labels Mar 25, 2024
@abonander abonander changed the title Json/Jsonb support for PostgreSQL is broken Document how to use Json<T> with the query macros Mar 25, 2024
@ElysaSrc
Copy link
Author

It is indeed working as intended when the types are set in the request.
Sorry for the false bug.

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

No branches or pull requests

2 participants