gpt4 book ai didi

rust - 为泛型编写 Diesel CRUD 操作

转载 作者:行者123 更新时间:2023-11-29 08:06:59 27 4
gpt4 key购买 nike

我正在尝试编写一个 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);
}

问题是编译器提示 C 缺少一些特征边界和 &C.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

非常感谢!

最佳答案

我终于通过定义以下特征边界解决了问题!

fn create<C, T>(model: C, pool: DBPool)
where
T: diesel::associations::HasTable,
<T::Table as diesel::QuerySource>::FromClause:
diesel::query_builder::QueryFragment<diesel::pg::Pg>,
C: diesel::Insertable<T::Table>,
C::Values: diesel::insertable::CanInsertInSingleQuery<diesel::pg::Pg>
+ diesel::query_builder::QueryFragment<diesel::pg::Pg>,
{
let conn = pool.get().unwrap();

diesel::insert_into(T::table())
.values(model)
.execute(&conn);
}

create::<UserCreate, users::table>(user, pool);

基本上,TableInsertable 需要一对额外的边界。如果可以直接从 Insertable 获取表格,避免在函数定义中使用其他类型,那就太好了,但我可以使用它:)

关于rust - 为泛型编写 Diesel CRUD 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58589018/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com