我正在尝试编写一个 Rust crate,它在使用Diesel创建简单的 CRUD 操作时从用户那里删除一些样板代码
例如,如果您有这样的柴油机Insertable
:
#[derive(Insertable)]
#[table_name = "users"]
pub struct UserCreate<'a> {
pub email: String,
pub hash: &'a [u8],
pub first_name: Option<String>,
pub family_name: Option<String>,
}
我希望 crate 用户只写create<UserCreate>(model, pool)
,将结构字段插入数据库行。
为此,我编写了以下函数签名(例如简化):
fn create<'a, C: 'a>(model: C, pool: DBPool)
where
C: diesel::Identifiable,
&'a C: diesel::Insertable<C::Table>,
{
let conn = pool.get().unwrap();
diesel::insert_into(C::table())
.values(&model)
.execute(&conn);
}
问题是编译器抱怨一些缺失的特征边界 forC
和&C
at.execute(&conn)
我不太确定如何将它们放在where
子句中,可能还有一种我不知道的更简单的方法。任何提示都非常受欢迎!
编译器输出:
error[E0277]: the trait bound `<<C as diesel::associations::HasTable>::Table as diesel::QuerySource>::FromClause: diesel::query_builder::QueryFragment<_>` is not satisfied
--> database/src/users/models.rs:46:10
|
46 | .execute(&conn);
| ^^^^^^^ the trait `diesel::query_builder::QueryFragment<_>` is not implemented for `<<C as diesel::associations::HasTable>::Table as diesel::QuerySource>::FromClause`
|
= help: the following implementations were found:
<&'a T as diesel::query_builder::QueryFragment<DB>>
= note: required because of the requirements on the impl of `diesel::query_builder::QueryFragment<_>` for `diesel::query_builder::InsertStatement<<C as diesel::associations::HasTable>::Table, <&C as diesel::Insertable<<C as diesel::associations::HasTable>::Table>>::Values>`
= note: required because of the requirements on the impl of `diesel::query_dsl::load_dsl::ExecuteDsl<_, _>` for `diesel::query_builder::InsertStatement<<C as diesel::associations::HasTable>::Table, <&C as diesel::Insertable<<C as diesel::associations::HasTable>::Table>>::Values>`
error[E0277]: the trait bound `<&C as diesel::Insertable<<C as diesel::associations::HasTable>::Table>>::Values: diesel::query_builder::QueryFragment<_>` is not satisfied
--> database/src/users/models.rs:46:10
|
46 | .execute(&conn);
| ^^^^^^^ the trait `diesel::query_builder::QueryFragment<_>` is not implemented for `<&C as diesel::Insertable<<C as diesel::associations::HasTable>::Table>>::Values`
|
= help: the following implementations were found:
<&'a T as diesel::query_builder::QueryFragment<DB>>
= note: required because of the requirements on the impl of `diesel::query_builder::QueryFragment<_>` for `diesel::query_builder::InsertStatement<<C as diesel::associations::HasTable>::Table, <&C as diesel::Insertable<<C as diesel::associations::HasTable>::Table>>::Values>`
= note: required because of the requirements on the impl of `diesel::query_dsl::load_dsl::ExecuteDsl<_, _>` for `diesel::query_builder::InsertStatement<<C as diesel::associations::HasTable>::Table, <&C as diesel::Insertable<<C as diesel::associations::HasTable>::Table>>::Values>`
error[E0277]: the trait bound `<&C as diesel::Insertable<<C as diesel::associations::HasTable>::Table>>::Values: diesel::insertable::CanInsertInSingleQuery<_>` is not satisfied
--> database/src/users/models.rs:46:10
|
46 | .execute(&conn);
| ^^^^^^^ the trait `diesel::insertable::CanInsertInSingleQuery<_>` is not implemented for `<&C as diesel::Insertable<<C as diesel::associations::HasTable>::Table>>::Values`
|
= help: the following implementations were found:
<&'a T as diesel::insertable::CanInsertInSingleQuery<DB>>
= note: required because of the requirements on the impl of `diesel::query_builder::QueryFragment<_>` for `diesel::query_builder::InsertStatement<<C as diesel::associations::HasTable>::Table, <&C as diesel::Insertable<<C as diesel::associations::HasTable>::Table>>::Values>`
= note: required because of the requirements on the impl of `diesel::query_dsl::load_dsl::ExecuteDsl<_, _>` for `diesel::query_builder::InsertStatement<<C as diesel::associations::HasTable>::Table, <&C as diesel::Insertable<<C as diesel::associations::HasTable>::Table>>::Values>`
error: aborting due to 3 previous errors
非常感谢!