我正在尝试使用通用 Diesel 函数来缩小重复性任务,例如基于主键删除一行。
我得到了相对较快的行的通用插入,但删除查询似乎相当困难。我尝试通过使用find()
和来解决它filter()
。我还咨询了类似的主题1和2,但没有成功。
使用find
use diesel::prelude::*;
use diesel::query_dsl::methods::FindDsl;
use std::error::Error;
pub struct DB {
conn: SqliteConnection,
}
impl DB {
pub fn remove_row<'a, T>(&self, table: T, pk: &'a str) -> Result<(), Box<Error>>
where
T: FindDsl<&'a str>,
<T as FindDsl<&'a str>>::Output: diesel::Identifiable,
<T as FindDsl<&'a str>>::Output: diesel::associations::HasTable,
{
diesel::delete(table.find(pk)).execute(&self.conn)?;
Ok(())
}
}
这会导致以下我无法解释的错误:
error[E0275]: overflow evaluating the requirement `_: std::marker::Sized`
--> src/db/mod.rs:103:3
|
103 | diesel::delete (table.find (pk)) .execute (&self.conn) ?;
| ^^^^^^^^^^^^^^
|
= help: consider adding a `#![recursion_limit="128"]` attribute to your crate
= note: required because of the requirements on the impl of `diesel::query_dsl::filter_dsl::FilterDsl<_>` for `<<<T as diesel::query_dsl::filter_dsl::FindDsl<&'a str>>::Output as diesel::associations::HasTable>::Table as diesel::query_builder::AsQuery>::Query`
= note: required because of the requirements on the impl of `diesel::query_builder::IntoUpdateTarget` for `<T as diesel::query_dsl::filter_dsl::FindDsl<&'a str>>::Output`
= note: required by `diesel::delete`
使用filter()
use diesel::prelude::*;
use diesel::query_dsl::methods::FilterDsl;
use std::error::Error;
pub struct DB {
conn: SqliteConnection,
}
impl DB {
pub fn remove_row<T>(&self, table: T, pk: &str) -> Result<(), Box<Error>>
where
T: FilterDsl<bool>,
<T as FilterDsl<bool>>::Output: diesel::Identifiable,
<T as FilterDsl<bool>>::Output: diesel::associations::HasTable,
{
diesel::delete(table.filter(id.eq(pk))).execute(&self.conn)?;
Ok(())
}
}
除了前面的错误,这里还有一条关于id
在数据结构中未知的错误信息。我可以考虑一个缺失的特征,它保证了该行的存在,但我还没有发现任何关于这种行为的信息。
error[E0425]: cannot find value `id` in this scope
--> src/db/mod.rs:117:33
|
117 | diesel::delete (table.filter (id.eq (pk))) .execute (&self.conn) ?;
| ^^ not found in this scope
help: possible candidates are found in other modules, you can import them into scope
|
4 | use crate::db::schema::events::columns::id;
|
4 | use crate::db::schema::ignored_events::columns::id;
|
4 | use crate::db::schema::locations::columns::id;
|
4 | use std::process::id;
error[E0275]: overflow evaluating the requirement `_: std::marker::Sized`
--> src/db/mod.rs:117:3
|
117 | diesel::delete (table.filter (id.eq (pk))) .execute (&self.conn) ?;
| ^^^^^^^^^^^^^^
|
= help: consider adding a `#![recursion_limit="128"]` attribute to your crate
= note: required because of the requirements on the impl of `diesel::query_dsl::filter_dsl::FilterDsl<_>` for `<<<T as diesel::query_dsl::filter_dsl::FilterDsl<bool>>::Output as diesel::associations::HasTable>::Table as diesel::query_builder::AsQuery>::Query`
= note: required because of the requirements on the impl of `diesel::query_builder::IntoUpdateTarget` for `<T as diesel::query_dsl::filter_dsl::FilterDsl<bool>>::Output`
= note: required by `diesel::delete`