创建要由pgpool”,“ mysqlpool”等实现的特征。

发布于 2025-01-22 22:14:30 字数 1404 浏览 3 评论 0原文

上实现的特征

#[async_trait]
trait MyTrait {
    const QUERY: &'static str;

    fn fetch(&self) -> Option<Self> {
        sqlx::query(Self::QUERY)
            .fetch_one(self)
            .await
            .map(|row| row.get(0))
            .ok()
    }
}

#[async_trait]
impl MyTrait for sqlx::PgPool {
    const QUERY: &'static str = "SELECT value FROM table WHERE id = 10";
}

error[E0277]: the trait bound `&Self: sqlx::Executor<'_>` is not satisfied
   --> service/src/lib.rs:265:24
    |
265 |             .fetch_one(self)
    |              --------- ^^^^ the trait `sqlx::Executor<'_>` is not implemented for `&Self`
    |              |
    |              required by a bound introduced by this call
    |
note: required by a bound in `sqlx::query::Query::<'q, DB, A>::fetch_one`

我正在使用sqlx来定义要在pgpoolmysqlpool等 尝试添加where self:sqlx :: executor&lt;'...,datbase = db&gt;及其多种变化,但不幸的是没有可用。

我目前正在尝试mytrait&lt;'p,db:sqlx ::数据库&gt; where&amp;'p self:sqlx :: executor&lt;'p,database = db&gt; s self:'p with inasimenation intermantion squy&lt;'_,sqlx :: postgres&gt;对于pgpool,但这给出了错误键入不匹配,以sqlx :: epecutor&lt;'_&gt;&gt; :: database == postgres`

告诉我我需要修改代码以进行编译,我真的很感激,谢谢。

I'm using SQLx to define a trait that is to be implemented on PgPool, MySqlPool, etc. and looks something like this:

#[async_trait]
trait MyTrait {
    const QUERY: &'static str;

    fn fetch(&self) -> Option<Self> {
        sqlx::query(Self::QUERY)
            .fetch_one(self)
            .await
            .map(|row| row.get(0))
            .ok()
    }
}

#[async_trait]
impl MyTrait for sqlx::PgPool {
    const QUERY: &'static str = "SELECT value FROM table WHERE id = 10";
}

This does not compile with:

error[E0277]: the trait bound `&Self: sqlx::Executor<'_>` is not satisfied
   --> service/src/lib.rs:265:24
    |
265 |             .fetch_one(self)
    |              --------- ^^^^ the trait `sqlx::Executor<'_>` is not implemented for `&Self`
    |              |
    |              required by a bound introduced by this call
    |
note: required by a bound in `sqlx::query::Query::<'q, DB, A>::fetch_one`

I've tried adding where Self: sqlx::Executor<'..., Datbase = DB> and numerous variations of it but to no avail unfortunately.

I'm currently trying something like MyTrait<'p, DB: sqlx::Database> where &'p Self: sqlx::Executor<'p, Database = DB>, Self: 'p with implementation impl MyTrait<'_, sqlx::Postgres> for PgPool but this gives the error type mismatch resolving `<&Self as sqlx::Executor<'_>>::Database == Postgres`

If anybody could tell me how I need to modify my code in order for it to compile I would really appreciate it, thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文