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

Clarify usage of Json/Jsonb in query macros #3447

Merged
merged 5 commits into from
Aug 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions sqlx-core/src/types/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,35 @@ use crate::types::Type;
/// dewey_decimal: sqlx::types::Json<HashMap<String, Book>>
/// }
/// ```
///
/// If the query macros are used, it is necessary to tell the macro to use
/// the `Json` adapter by using the type override syntax
/// ```rust,ignore
/// # async fn example3() -> sqlx::Result<()> {
/// # let mut conn: sqlx::PgConnection = unimplemented!();
/// #[derive(sqlx::FromRow)]
/// struct Book {
/// title: String,
/// }
///
/// #[derive(sqlx::FromRow)]
/// struct Author {
/// name: String,
/// books: sqlx::types::Json<Book>,
/// }
/// // Note the type override in the query string
/// let authors = sqlx::query_as!(
/// Author,
/// r#"
/// SELECT name, books as "books: Json<Book>"
/// FROM authors
/// "#
/// )
/// .fetch_all(&mut conn)
/// .await?;
/// # Ok(())
/// # }
/// ```
#[derive(
Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Serialize, Deserialize,
)]
Expand Down