我们如何在 sqlx rust 中定义 jsonb 和 UUID 字段?

Asn*_*ari 4 rust rust-sqlx

我有一个 Postgres 表,其中包含三个字段 id,即 a bigserialmetaajsonb字段和 uuidUUID字段。

pub struct MetaLogs {

    pub id:i64,
    pub uuid: <what type should I give here > 
    pub meta: < What type should I give here > 
}
Run Code Online (Sandbox Code Playgroud)

我正在使用sqlxORM 进行Rust. 虽然我明白我必须添加

features = [ "runtime-tokio", "macros" ,"postgres","json","uuid"]
Run Code Online (Sandbox Code Playgroud)

我不知道之后如何继续

w08*_*08r 6

sqlx为 PostgreSQL提供JsonUuid类型实现。请参阅uuid.rsjson.rs

请注意,Json 类型将在内部解析为 jsonb,这正如您所期望的那样。

样本:

use sqlx::{types::Uuid, types::Json};
pub struct MetaLogs {
    pub id: i64,
    pub uuid: Uuid, 
    pub meta: Json,
}
Run Code Online (Sandbox Code Playgroud)

  • 这需要将“uuid”添加到功能中。 (4认同)