我试图创建一些方便的方法,这样我就可以在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`
注解掉get
时delete
的错误:
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)
编译
1条答案
按热度按时间sh7euo9m1#
我在尝试使用Enums时就遇到了这种情况。
我在枚举代码中使用了这样的代码:
使用
diesel-derive-enum
crate forDbEnum
derive.我的问题是,我使用AsExpression
与DbEnum
,所以它不能解决使用哪个(我想)删除
AsExpression
并将DbEnum
与ExistingTypePath
一起使用解决了这个问题。