Skip to content

Commit b7c7da7

Browse files
author
Leon Lux
committed
add information and example on using json in query macros
1 parent 6f29056 commit b7c7da7

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

sqlx-core/src/types/json.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,32 @@ use crate::types::Type;
5151
/// dewey_decimal: sqlx::types::Json<HashMap<String, Book>>
5252
/// }
5353
/// ```
54+
///
55+
/// If the query macros are used, it is necessary to tell the macro to use
56+
/// the `Json` adapter by using the type override syntax
57+
/// ```
58+
/// #[derive(sqlx::FromRow]
59+
/// struct Book {
60+
/// title: String,
61+
/// }
62+
///
63+
/// #[derive(sqlx::FromRow)]
64+
/// struct Author {
65+
/// name: String,
66+
/// books: sqlx::types::Json<Book>,
67+
/// }
68+
/// // Note the type override in the query string
69+
/// let authors = sqlx::query_as!(
70+
/// Author,
71+
/// r#"
72+
/// SELECT name, books as "books: Json<Book>"
73+
/// FROM authors
74+
/// "#
75+
/// )
76+
/// .fetch_all(pool)
77+
/// .await?;
78+
///
79+
/// ```
5480
#[derive(
5581
Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Serialize, Deserialize,
5682
)]

0 commit comments

Comments
 (0)