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

5.1k views Asked by At

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

1

There are 1 answers

1
w08r On BEST ANSWER

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,
}