rust 溢出评估需求`_:std::marker::Sized`在实现Diesel结构体的get和delete特征时

cgvd09ve  于 2023-04-21  发布在  其他
关注(0)|答案(1)|浏览(134)

我试图创建一些方便的方法,这样我就可以在Banana的示例上调用banana.get(&conn, &pk)banana.delete(&conn),但是我不能正确地获得trait边界,并且似乎一直得到递归错误:

货物清单

[package]
name = "m"
version = "0.1.0"
edition = "2018"

[dependencies]
diesel = { version = "1.4.5", features = ["postgres"] }

来源/lib.rs

use diesel::PgConnection;
use diesel::{
    associations::{HasTable, Identifiable},
    delete,
    query_dsl::QueryDsl,
    result::QueryResult,
};

pub trait DbModel: HasTable + Identifiable + QueryDsl {
    fn get(
        conn: &PgConnection,
        pk: &<Self as Identifiable>::Id,
    ) -> QueryResult<Self> {
        Self::table().find(pk).first(conn)
    }

    fn delete(&self, conn: &PgConnection) -> QueryResult<usize> {
        delete(self).execute(conn)
    }
}

生成错误:

error[E0275]: overflow evaluating the requirement `_: std::marker::Sized`
  --> src/lib.rs:14:23
   |
14 |         Self::table().find(pk).first(conn)
   |                       ^^^^
   |
   = help: consider adding a `#![recursion_limit="256"]` attribute to your crate (`m`)
   = note: required because of the requirements on the impl of `diesel::query_dsl::filter_dsl::FilterDsl<_>` for `<<Self as diesel::associations::HasTable>::Table as diesel::query_builder::AsQuery>::Query`

注解掉getdelete的错误:

error[E0275]: overflow evaluating the requirement `_: std::marker::Sized`
   --> src/lib.rs:18:9
    |
18  |         delete(self).execute(conn)
    |         ^^^^^^
    | 
   ::: /home/danj/.cargo/registry/src/github.com-1ecc6299db9ec823/diesel-1.4.5/src/query_builder/functions.rs:135:18
    |
135 | pub fn delete<T: IntoUpdateTarget>(source: T) -> DeleteStatement<T::Table, T::WhereClause> {
    |                  ---------------- required by this bound in `diesel::query_builder::functions::delete`
    |
    = help: consider adding a `#![recursion_limit="256"]` attribute to your crate (`m`)
    = note: required because of the requirements on the impl of `diesel::query_dsl::filter_dsl::FilterDsl<_>` for `<<Self as diesel::associations::HasTable>::Table as diesel::query_builder::AsQuery>::Query`
    = note: required because of the requirements on the impl of `diesel::query_builder::update_statement::target::IntoUpdateTarget` for `&Self`

使用cargo 1.46.0-nightly (c26576f9a 2020-06-23)编译

sh7euo9m

sh7euo9m1#

我在尝试使用Enums时就遇到了这种情况。
我在枚举代码中使用了这样的代码:

#[derive(
    Debug, Clone, Serialize, Deserialize, GraphQLEnum, DbEnum
)]
#[ExistingTypePath = "crate::models::schema::sql_types::UnitStatus"]
pub enum UnitStatus {
    NotStarted,
    InProgress,
    Completed,
}

使用diesel-derive-enum crate for DbEnum derive.我的问题是,我使用AsExpressionDbEnum,所以它不能解决使用哪个(我想)
删除AsExpression并将DbEnumExistingTypePath一起使用解决了这个问题。

相关问题