Unable to select a jsonb column to a struct with a Json<_> field

54 Views Asked by At

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_form and comment_form from the struct the request compiles properly.
  • I checked that the json feature is activated on sqlx
  • I checked that the raw_value feature is activated on serde_json
0

There are 0 best solutions below