Depending on some settings, I want to filter out the occurrences of an event. Because I have boxed the query, it doesn’t let me use it to delete them. The code in question is available at https://github.com/Y0hy0h/lindyhop-aachen/pull/41.
There might be a better approach to creating the conditional query. Any ideas are welcome.
Part of the model:
#[derive(Queryable, Insertable, Debug, Identifiable, Clone, PartialEq, AsChangeset)]
#[table_name = "events"]
pub struct SqlEvent {
pub id: SqlId<Event>,
pub title: String,
pub teaser: String,
pub description: String,
}
#[derive(
Queryable, Insertable, Clone, Debug, Identifiable, PartialEq, AsChangeset, Associations,
)]
#[belongs_to(SqlEvent, foreign_key = "event_id")]
#[belongs_to(SqlLocation, foreign_key = "location_id")]
#[table_name = "occurrences"]
pub struct SqlOccurrence {
pub id: SqlId<Occurrence>,
pub event_id: SqlId<Event>,
pub start: NaiveDateTime,
pub duration: i32,
pub location_id: SqlId<Location>,
}
The last line of this function’s excerpt is causing errors:
use db::schema::occurrences::dsl::start;
let mut associated_occurrences = SqlOccurrence::belonging_to(&sql_previous).into_boxed();
if let Some(datetime) = filter.after {
associated_occurrences= associated_occurrences.filter(start.gt(datetime));
}
if let Some(datetime) = filter.before {
associated_occurrences= associated_occurrences.filter(start.lt(datetime));
}
let previous_occurrences: Vec<OccurrenceWithLocation> = associated_occurrences
.load::<SqlOccurrence>(&*self.0)?
.into_iter()
.map(|sql_occurrence| {
let (_, occurrence) = sql_occurrence.into(); // I have a From implementation that converts the SqlOccurrence into (Id<Occurrence>, Occurrence), which are user-facing types.
occurrence
})
.collect();
diesel::delete(associated_occurrences).execute(&*self.0)?;
The errors are:
error[E0277]: the trait bound `diesel::query_builder::BoxedSelectStatement<'_, (diesel::sql_types::Binary, diesel::sql_types::Binary, diesel::sql_types::Timestamp, diesel::sql_types::Integer, diesel::sql_types::Binary), store::db::schema::occurrences::table, diesel::sqlite::Sqlite>: diesel::Identifiable` is not satisfied
--> src\store\mod.rs:406:9
|
406 | diesel::delete(associated_occurrences).execute(&*self.0)?;
| ^^^^^^^^^^^^^^ the trait `diesel::Identifiable` is not implemented
for `diesel::query_builder::BoxedSelectStatement<'_, (diesel::sql_types::Binary,
diesel::sql_types::Binary, diesel::sql_types::Timestamp, diesel::sql_types::Integer, diesel::sql_types::Binary), store::db::schema::occurrences::table, diesel::sqlite::Sqlite>`
|
= note: required because of the requirements on the impl of `diesel::query_builder::IntoUpdateTarget` for `diesel::query_builder::BoxedSelectStatement<'_, (diesel::sql_types::Binary, diesel::sql_types::Binary, diesel::sql_types::Timestamp, diesel::sql_types::Integer, diesel::sql_types::Binary), store::db::schema::occurrences::table, diesel::sqlite::Sqlite>`
= note: required by `diesel::delete`
error[E0277]: the trait bound `diesel::query_builder::BoxedSelectStatement<'_, (diesel::sql_types::Binary, diesel::sql_types::Binary, diesel::sql_types::Timestamp, diesel::sql_types::Integer, diesel::sql_types::Binary), store::db::schema::occurrences::table, diesel::sqlite::Sqlite>: diesel::associations::HasTable` is not satisfied
--> src\store\mod.rs:406:9
|
406 | diesel::delete(associated_occurrences).execute(&*self.0)?;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `diesel::associations::HasTable` is not implemented for `diesel::query_builder::BoxedSelectStatement<'_, (diesel::sql_types::Binary, diesel::sql_types::Binary, diesel::sql_types::Timestamp, diesel::sql_types::Integer, diesel::sql_types::Binary), store::db::schema::occurrences::table, diesel::sqlite::Sqlite>`