I'm generating Diesel filters at runtime (using a similar approach to this question). I have a function make_filters
which generates filters for the table tunnel
:
fn make_filters() -> Vec<Box<dyn BoxableExpression<tunnel::table, Pg, SqlType = Bool>>>
I'd like these filters to be applied to either the table tunnel
or to the join of tunnel
and connection
. Like this:
let mut query = tunnel::table.left_join(connection::table).into_boxed();
for filter in filters.as_sql_where() {
query = query.filter(filter);
}
But when I try, I get this error:
^^^ the trait
`diesel::AppearsOnTable<diesel::query_source::joins::JoinOn<diesel::query_source::joins::Join<tunnel::table, connection::table, diesel::query_source::joins::LeftOuter>, diesel::expression::operators::Eq<diesel::expression::nullable::Nullable<connection::columns::tunnel_id>, diesel::expression::nullable::Nullable<tunnel::columns::id>>>>`
is not implemented for `dyn diesel::BoxableExpression<tunnel::table, diesel::pg::Pg, SqlType = diesel::sql_types::Bool>`
This works fine if query is just tunnel::table.into_boxed()
, so I think I need to generalize the typesignature of make_filters
.
Minimal example here.
As the filters can be applied to both, the joined tables and the tunnel
table the preferred solution here is to just apply the filters before joining the tables. So something like this should work fine:
let mut query = tunnel::table.into_boxed();
for filter in filters.as_sql_where() {
query = query.filter(filter);
}
let query = query.left_join(connection::table);