rustrust-sqlx

How do we define a jsonb and UUID field in sqlx rust?


I have a Postgres table having three fields id which is a bigserial, meta a jsonb field and a uuid UUID field.

pub struct MetaLogs {

    pub id:i64,
    pub uuid: <what type should I give here > 
    pub meta: < What type should I give here > 
}

I am using sqlx ORM for Rust. Although I understood that I have to add

features = [ "runtime-tokio", "macros" ,"postgres","json","uuid"]

I was unable to figure it out how to proceed after that


Solution

  • sqlx provides both Json and Uuid type implementations for PostgreSQL. See uuid.rs and json.rs.

    Note the Json type will resolve to jsonb internally which is as you'd expect.

    Sample:

    use sqlx::{types::Uuid, types::Json};
    pub struct MetaLogs {
        pub id: i64,
        pub uuid: Uuid, 
        pub meta: Json,
    }