微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何将枚举的 Vec 定义为 rust sqlx 模型中的字段

如何解决如何将枚举的 Vec 定义为 rust sqlx 模型中的字段

我正在尝试将 Postgres 中的数组字段加载到 Rust 结构中,如下所示

use sqlx::{Pool,PgConnection,PgPool,Fromrow};
use sqlx::postgres::PgQueryAs;

#[derive(copy,Clone,sqlx::Type)]
#[sqlx(rename = "VARCHAR")]
#[sqlx(rename_all = "snake_case")]
enum Coupon {
    ChristmasSaleCoupon,NewYearSaleCoupon,}

#[derive(Fromrow,Clone)]
struct UserCouponMap {
    pub id: i64,pub user_id: i64,pub coupons: Vec<Coupon>,}

impl UserCouponMap {
    pub async fn get_for_userid(db_pool: Pool<PgConnection>,user_id: i64) -> Vec<UserCouponMap> {
        let user_coupon_map: Vec<UserCouponMap> = sqlx::query_as("SELECT * FROM user_coupon_map WHERE user_id = $1")
            .bind(user_id)
            .fetch_all(db_pool)
            .await
            .expect("Failed to fetch user coupon map");
        user_coupon_map
    }
}


#[tokio::main]
async fn fetch_coupons_for_user_id(user_id: i64) {
    let pool = PgPool::new("postgresql://asnim@dbhost:5732").await.expect("expected unwrap to succeed");
    let user_coupon_map = UserCouponMap::get_for_userid(pool,user_id).await;
}

fn main() {
    fetch_coupons_for_user_id(20);
}

根据数组 documentation,我已经为 sqlx::Type 实现了 Coupon

仍然,编译器说,某些特征不满足。

   Compiling playground v0.1.0 (/Users/asnimansari/CLionProjects/playground)
error[E0277]: the trait bound `Vec<Coupon>: Type<Postgres>` is not satisfied
  --> src/main.rs:23:14
   |
23 |             .fetch_all(db_pool)
   |              ^^^^^^^^^ the trait `Type<Postgres>` is not implemented for `Vec<Coupon>`
   |
   = help: the following implementations were found:
             <Vec<&[u8]> as Type<Postgres>>
             <Vec<&str> as Type<Postgres>>
             <Vec<(T1,T2)> as Type<Postgres>>
             <Vec<(T1,T2,T3)> as Type<Postgres>>
           and 29 others
   = note: required because of the requirements on the impl of `for<'c> Fromrow<'c,PgRow<'c>>` for `UserCouponMap`

error[E0277]: the trait bound `[Coupon]: Type<Postgres>` is not satisfied
  --> src/main.rs:23:14
   |
23 |             .fetch_all(db_pool)
   |              ^^^^^^^^^ the trait `Type<Postgres>` is not implemented for `[Coupon]`
   |
   = help: the following implementations were found:
             <[&[u8]] as Type<Postgres>>
             <[&str] as Type<Postgres>>
             <[(T1,T2)] as Type<Postgres>>
             <[(T1,T3)] as Type<Postgres>>
           and 29 others
   = note: required because of the requirements on the impl of `sqlx::decode::Decode<'_,Postgres>` for `Vec<Coupon>`
   = note: required because of the requirements on the impl of `for<'c> Fromrow<'c,PgRow<'c>>` for `UserCouponMap`

error: aborting due to 3 prevIoUs errors

For more information about this error,try `rustc --explain E0277`.
error: Could not compile `playground`

To learn more,run the command again with --verbose.


在这里遗漏了什么?

我的 Cargo 文件具有以下依赖项

sqlx = { version = "0.3.5",default-features = false,features = ["runtime-tokio","macros","postgres","all-type"] }
tokio = { version = "0.2.21",features = ["full"] }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。