I have 2 tables in my database: users
and products
, both of which have a TEXT name
column.
I can query the users table using the following query:
fn search_user_name(
conn: &Conn, // type alias for pooled connection
search: String,
) -> Result<Vec<User>, ()> {
let result = users
.filter(name.like(search))
.load(conn)
.map_err(|_| ())?;
Ok(result)
}
However, I'd like to be generic over both the table and column. So far I have the following:
pub fn generic_search<T, C, M>(
conn: &Conn,
table: T,
column: C,
search: String,
) -> Result<Vec<M>, ()>
where
C: TextExpressionMethods + Expression<SqlType = Text>,
T: FilterDsl<Like<C, Bound<Text, String>>, Output = M>,
Filter<T, Like<C, Bound<Text, String>>>: LoadQuery<SqliteConnection, M>,
{
let result = table
.filter(column.like(search))
.load(conn)
.map_err(|_| ())?;
Ok(result)
}
I feel like these are the constraints that actually describe behaviour of the function, but this gives an error:
overflow evaluating the requirement `<T as diesel::query_dsl::filter_dsl::FilterDsl<diesel::expression::operators::Like<C, diesel::expression::bound::Bound<diesel::sql_types::Text, std::string::String>>>>::Output == _`
Setting #![recursion_limit = "100000"]
didn't fix this, so my guess is that there's some infinitely recursive loop going on somewhere, but I can't see what it is.
I came across Generic function using Diesel causes overflow , which looks similar to my question, but I am either already using the solutions provided in that answer, or they don't apply to this case.
I feel like there's something simple I'm missing, but I haven't been able to find it