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

问题描述

我有一个包含三个字段的Postgres表 id,是bigserialMetajsonb字段,是uuid UUID字段。

pub struct MetaLogs {

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

我在sqlx中使用Rust ORM。 虽然我知道我必须添加

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

在那之后我无法弄清楚如何进行

解决方法

sqlx为PostgreSQL提供了JsonUuid类型的实现。参见:

https://github.com/launchbadge/sqlx/blob/7c4c1856988ecc2ac2b8892453aeee09c245257f/sqlx-core/src/postgres/types/uuid.rs

https://github.com/launchbadge/sqlx/blob/f0f93c4f79d18ff85af6364837fc8157ec444125/sqlx-core/src/postgres/types/json.rs

请注意,Json类型将在内部解析为jsonb,这与您期望的一样。

示例:

use sqlx::{types::Uuid,types::Json};
pub struct MetaLogs {

    pub id:i64,pub uuid: Uuid,pub meta: Json,}