I have the following table definition:
CREATE TABLE families (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
title VARCHAR(255) NOT NULL,
entity_form JSONB NOT NULL,
comment_form JSONB NOT NULL
);
And I have the following struct defined:
#[derive(FromRow, Deserialize, Serialize, Debug)]
pub struct Family {
pub id: Uuid,
pub title: String,
pub entity_form: Json<Form>,
pub comment_form: Json<Form>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Form {
// ... fields ...
}
If I try to SELECT a row using this code:
pub async fn get(given_id: Uuid, conn: &mut PgConnection) -> Result<Family, AppError> {
sqlx::query_as!(
Family,
r#"
SELECT id, title, entity_form, comment_form
FROM families
WHERE id = $1
"#,
given_id
)
.fetch_one(conn)
.await
.map_err(AppError::DatabaseError)
}
The compiler fails with this trace:
error[E0277]: the trait bound `sqlx::types::Json<family::Form>: From<JsonValue>` is not satisfied
--> src/models/family.rs:181:9
|
181 | / sqlx::query_as!(
182 | | Family,
183 | | r#"
184 | | SELECT id, title, entity_form, comment_form
... |
188 | | given_id
189 | | )
| |_________^ the trait `From<JsonValue>` is not implemented for `sqlx::types::Json<family::Form>`
|
Why is the compiler failing ? I have the feeling my example is not that far from the documentation of sqlx for json.
- If I remove the
entity_formandcomment_formfrom the struct the request compiles properly. - I checked that the
jsonfeature is activated onsqlx - I checked that the
raw_valuefeature is activated onserde_json