Getting error while diesel filtering with eq_any on optional field

Hi,

I am trying to filter a diesel query by using eq_any but it gives me error.

Error:

    the trait `ValidGrouping<()>` is not implemented for `CustomerServiceEnum` (error points to the filter function)

Code:

    data_query.filter(dsl::customer::props::customer_services.eq_any(filter_customer_services))

Info:

  • filter_customer_services is a Vec<String>
  • customer_services field is Option<CustomerServiceEnum> in the mode. Here is the definition of the enum:
    # [derive(
    Debug,
    Serialize,
    DbEnum,
    Deserialize,
    Copy,
    Clone,
    ToSchema,
    )] 
    # [ExistingTypePath = "DbCustomerService"] // Ref. to super::schema::sql_types::CustomerService
    # [serde(rename_all = "snake_case")]
    pub enum CustomerServiceEnum {
        X,
        Y,
        Z,
        Others,
    }

Cargo.toml

diesel = { version = "2.0.0", features = [
    "postgres",
    "uuid",
    "chrono",
    "serde_json",
    "64-column-tables",
] }
diesel-derive-enum = { version = "2.0.1", features = ["postgres"] }

schema.rs

#[derive(diesel::query_builder::QueryId, diesel::sql_types::SqlType)]
#[diesel(postgres_type(name = "customer_service"))]
pub struct CustomerService;
...
customer (id) {
    ...
    customer_services -> Nullable<Array<Nullable<CustomerService>>>,
    ...
}

repo.rs (where queries are defined)

...
let mut data_query = dsl::customer::table.into_boxed();
let data = data_query
    .order(sql::<diesel::sql_types::Bool>(&format!(
        "{} {}",
        order_by, order
    )))
    .limit(per_page)
    .offset(offset)
    .load::<DbCustomer>(c)?;
...

Full error:

error[E0277]: the trait bound `CustomerServiceEnum: diesel::Expression` is not satisfied
    --> database/src/customer/db/customer/repo.rs:1242:104
     |
1242 | ...                   data_query = data_query.filter(dsl::customer::props::customer_services.eq_any(filter_customer_services.clone()));
     |                                                                                              ^^^^^^ the trait `diesel::Expression` is not implemented for `CustomerServiceEnum`
     |
     = help: the following other types implement trait `diesel::Expression`:
               Box<T>
               diesel::expression::ops::numeric::Add<Lhs, Rhs>
               diesel::expression::ops::numeric::Sub<Lhs, Rhs>
               diesel::expression::ops::numeric::Mul<Lhs, Rhs>
               diesel::expression::ops::numeric::Div<Lhs, Rhs>
               now
               today
               In<T, U>
             and 3070 others
     = note: required for `CustomerServiceEnum` to implement `AsExpression<diesel::sql_types::Nullable<diesel::sql_types::Array<diesel::sql_types::Nullable<db_setup::diesel::schema::sql_types::CustomerService>>>>`
     = note: required for `Vec<CustomerServiceEnum>` to implement `AsInExpression<diesel::sql_types::Nullable<diesel::sql_types::Array<diesel::sql_types::Nullable<db_setup::diesel::schema::sql_types::CustomerService>>>>`

error[E0277]: the trait bound `CustomerServiceEnum: ValidGrouping<()>` is not satisfied
    --> database/src/customer/db/customer/repo.rs:1242:57
     |
1242 | ...                   data_query = data_query.filter(dsl::customer::props::customer_services.eq_any(filter_customer_services.clone()));
     |                                               ^^^^^^ the trait `ValidGrouping<()>` is not implemented for `CustomerServiceEnum`
     |
     = help: the following other types implement trait `ValidGrouping<GroupByClause>`:
               <Box<T> as ValidGrouping<GB>>
               <diesel::expression::ops::numeric::Add<Lhs, Rhs> as ValidGrouping<__GroupByClause>>
               <diesel::expression::ops::numeric::Sub<Lhs, Rhs> as ValidGrouping<__GroupByClause>>
               <diesel::expression::ops::numeric::Mul<Lhs, Rhs> as ValidGrouping<__GroupByClause>>
               <diesel::expression::ops::numeric::Div<Lhs, Rhs> as ValidGrouping<__GroupByClause>>
               <now as ValidGrouping<__GroupByClause>>
               <today as ValidGrouping<__GroupByClause>>
               <In<T, U> as ValidGrouping<__GroupByClause>>
             and 5735 others
     = note: required for `Many<diesel::sql_types::Nullable<diesel::sql_types::Array<diesel::sql_types::Nullable<db_setup::diesel::schema::sql_types::CustomerService>>>, CustomerServiceEnum>` to implement `ValidGrouping<()>`
     = note: 1 redundant requirement hidden
     = note: required for `In<customer_services, Many<diesel::sql_types::Nullable<diesel::sql_types::Array<diesel::sql_types::Nullable<db_setup::diesel::schema::sql_types::CustomerService>>>, CustomerServiceEnum>>` to implement `ValidGrouping<()>`
     = note: required for `diesel::expression::grouped::Grouped<In<customer_services, Many<diesel::sql_types::Nullable<diesel::sql_types::Array<diesel::sql_types::Nullable<db_setup::diesel::schema::sql_types::CustomerService>>>, CustomerServiceEnum>>>` to implement `NonAggregate`
     = note: required for `BoxedSelectStatement<'_, (Uuid, Integer, Jsonb, Text, Nullable<Text>, Nullable<Text>, ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ...), ..., ...>` to implement `FilterDsl<diesel::expression::grouped::Grouped<In<customer_services, Many<diesel::sql_types::Nullable<diesel::sql_types::Array<diesel::sql_types::Nullable<db_setup::diesel::schema::sql_types::CustomerService>>>, CustomerServiceEnum>>>>`
     = note: the full type name has been written to '/home/project_v1/target/debug/deps/database-520d3052fb489996.long-type-15439504204540865808.txt'

error[E0277]: the trait bound `CustomerServiceEnum: SelectableExpression<customers>` is not satisfied
    --> database/src/customer/db/customer/repo.rs:1242:57
     |
1242 | ...                   data_query = data_query.filter(dsl::customer::props::customer_services.eq_any(filter_customer_services.clone()));
     |                                               ^^^^^^ the trait `SelectableExpression<customers>` is not implemented for `CustomerServiceEnum`
     |
     = help: the following other types implement trait `SelectableExpression<QS>`:
               <Box<T> as SelectableExpression<QS>>
               <diesel::expression::ops::numeric::Add<Lhs, Rhs> as SelectableExpression<QS>>
               <diesel::expression::ops::numeric::Sub<Lhs, Rhs> as SelectableExpression<QS>>
               <diesel::expression::ops::numeric::Mul<Lhs, Rhs> as SelectableExpression<QS>>
               <diesel::expression::ops::numeric::Div<Lhs, Rhs> as SelectableExpression<QS>>
               <now as SelectableExpression<QS>>
               <today as SelectableExpression<QS>>
               <In<T, U> as SelectableExpression<QS>>
             and 16330 others
     = note: required for `Many<diesel::sql_types::Nullable<diesel::sql_types::Array<diesel::sql_types::Nullable<db_setup::diesel::schema::sql_types::CustomerService>>>, CustomerServiceEnum>` to implement `AppearsOnTable<customers>`
     = note: 2 redundant requirements hidden
     = note: required for `diesel::expression::grouped::Grouped<In<customer_services, Many<diesel::sql_types::Nullable<diesel::sql_types::Array<diesel::sql_types::Nullable<db_setup::diesel::schema::sql_types::CustomerService>>>, CustomerServiceEnum>>>` to implement `AppearsOnTable<customers>`
     = note: required for `BoxedSelectStatement<'_, (Uuid, Integer, Jsonb, Text, Nullable<Text>, Nullable<Text>, ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ...), ..., ...>` to implement `FilterDsl<diesel::expression::grouped::Grouped<In<customer_services, Many<diesel::sql_types::Nullable<diesel::sql_types::Array<diesel::sql_types::Nullable<db_setup::diesel::schema::sql_types::CustomerService>>>, CustomerServiceEnum>>>>`
     = note: the full type name has been written to '/home/project_v1/target/debug/deps/database-520d3052fb489996.long-type-15439504204540865808.txt'

error[E0277]: the trait bound `CustomerServiceEnum: ToSql<diesel::sql_types::Nullable<diesel::sql_types::Array<diesel::sql_types::Nullable<db_setup::diesel::schema::sql_types::CustomerService>>>, Pg>` is not satisfied
    --> database/src/customer/db/customer/repo.rs:1242:57
     |
1242 | ...                   data_query = data_query.filter(dsl::customer::props::customer_services.eq_any(filter_customer_services.clone()));
     |                                               ^^^^^^ the trait `ToSql<diesel::sql_types::Nullable<diesel::sql_types::Array<diesel::sql_types::Nullable<db_setup::diesel::schema::sql_types::CustomerService>>>, Pg>` is not implemented for `CustomerServiceEnum`
     |
     = help: the following other types implement trait `ToSql<A, DB>`:
               <CustomerServiceEnum as ToSql<db_setup::diesel::schema::sql_types::CustomerService, Pg>>
               <CustomerServiceEnum as ToSql<diesel::sql_types::Nullable<db_setup::diesel::schema::sql_types::CustomerService>, DB>>
     = note: required for `[CustomerServiceEnum]` to implement `ToSql<diesel::sql_types::Array<diesel::sql_types::Nullable<diesel::sql_types::Array<diesel::sql_types::Nullable<db_setup::diesel::schema::sql_types::CustomerService>>>>, Pg>`
     = note: 1 redundant requirement hidden
     = note: required for `Vec<CustomerServiceEnum>` to implement `ToSql<diesel::sql_types::Array<diesel::sql_types::Nullable<diesel::sql_types::Array<diesel::sql_types::Nullable<db_setup::diesel::schema::sql_types::CustomerService>>>>, Pg>`
     = note: required for `Many<diesel::sql_types::Nullable<diesel::sql_types::Array<diesel::sql_types::Nullable<db_setup::diesel::schema::sql_types::CustomerService>>>, CustomerServiceEnum>` to implement `QueryFragment<Pg, pg::backend::PgStyleArrayComparison>`
     = note: 4 redundant requirements hidden
     = note: required for `diesel::expression::grouped::Grouped<In<customer_services, Many<diesel::sql_types::Nullable<diesel::sql_types::Array<diesel::sql_types::Nullable<db_setup::diesel::schema::sql_types::CustomerService>>>, CustomerServiceEnum>>>` to implement `QueryFragment<Pg>`
     = note: required for `query_builder::where_clause::BoxedWhereClause<'_, Pg>` to implement `query_builder::where_clause::WhereAnd<diesel::expression::grouped::Grouped<In<customer_services, Many<diesel::sql_types::Nullable<diesel::sql_types::Array<diesel::sql_types::Nullable<db_setup::diesel::schema::sql_types::CustomerService>>>, CustomerServiceEnum>>>>`
     = note: required for `BoxedSelectStatement<'_, (Uuid, Integer, Jsonb, Text, Nullable<Text>, Nullable<Text>, ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ...), ..., ...>` to implement `FilterDsl<diesel::expression::grouped::Grouped<In<customer_services, Many<diesel::sql_types::Nullable<diesel::sql_types::Array<diesel::sql_types::Nullable<db_setup::diesel::schema::sql_types::CustomerService>>>, CustomerServiceEnum>>>>`
     = note: the full type name has been written to '/home/project_v1/target/debug/deps/database-520d3052fb489996.long-type-15439504204540865808.txt'

error[E0277]: the trait bound `CustomerServiceEnum: SelectableExpression<customers>` is not satisfied
    --> database/src/customer/db/customer/repo.rs:1243:65
     |
1243 | ...                   data_query_all = data_query_all.filter(dsl::customer::props::customer_services.eq_any(filter_customer_services.clone()));
     |                                                       ^^^^^^ the trait `SelectableExpression<customers>` is not implemented for `CustomerServiceEnum`
     |
     = help: the following other types implement trait `SelectableExpression<QS>`:
               <Box<T> as SelectableExpression<QS>>
               <diesel::expression::ops::numeric::Add<Lhs, Rhs> as SelectableExpression<QS>>
               <diesel::expression::ops::numeric::Sub<Lhs, Rhs> as SelectableExpression<QS>>
               <diesel::expression::ops::numeric::Mul<Lhs, Rhs> as SelectableExpression<QS>>
               <diesel::expression::ops::numeric::Div<Lhs, Rhs> as SelectableExpression<QS>>
               <now as SelectableExpression<QS>>
               <today as SelectableExpression<QS>>
               <In<T, U> as SelectableExpression<QS>>
             and 16330 others
     = note: required for `Many<diesel::sql_types::Nullable<diesel::sql_types::Array<diesel::sql_types::Nullable<db_setup::diesel::schema::sql_types::CustomerService>>>, CustomerServiceEnum>` to implement `AppearsOnTable<customers>`
     = note: 2 redundant requirements hidden
     = note: required for `diesel::expression::grouped::Grouped<In<customer_services, Many<diesel::sql_types::Nullable<diesel::sql_types::Array<diesel::sql_types::Nullable<db_setup::diesel::schema::sql_types::CustomerService>>>, CustomerServiceEnum>>>` to implement `AppearsOnTable<customers>`
     = note: required for `BoxedSelectStatement<'_, (Uuid, Integer, Jsonb, Text, Nullable<Text>, Nullable<Text>, ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ...), ..., ...>` to implement `FilterDsl<diesel::expression::grouped::Grouped<In<customer_services, Many<diesel::sql_types::Nullable<diesel::sql_types::Array<diesel::sql_types::Nullable<db_setup::diesel::schema::sql_types::CustomerService>>>, CustomerServiceEnum>>>>`
     = note: the full type name has been written to '/home/project_v1/target/debug/deps/database-520d3052fb489996.long-type-4311282295673555358.txt'

error[E0277]: the trait bound `CustomerServiceEnum: ToSql<diesel::sql_types::Nullable<diesel::sql_types::Array<diesel::sql_types::Nullable<db_setup::diesel::schema::sql_types::CustomerService>>>, Pg>` is not satisfied
    --> database/src/customer/db/customer/repo.rs:1243:65
     |
1243 | ...                   data_query_all = data_query_all.filter(dsl::customer::props::customer_services.eq_any(filter_customer_services.clone()));
     |                                                       ^^^^^^ the trait `ToSql<diesel::sql_types::Nullable<diesel::sql_types::Array<diesel::sql_types::Nullable<db_setup::diesel::schema::sql_types::CustomerService>>>, Pg>` is not implemented for `CustomerServiceEnum`
     |
     = help: the following other types implement trait `ToSql<A, DB>`:
               <CustomerServiceEnum as ToSql<db_setup::diesel::schema::sql_types::CustomerService, Pg>>
               <CustomerServiceEnum as ToSql<diesel::sql_types::Nullable<db_setup::diesel::schema::sql_types::CustomerService>, DB>>
     = note: required for `[CustomerServiceEnum]` to implement `ToSql<diesel::sql_types::Array<diesel::sql_types::Nullable<diesel::sql_types::Array<diesel::sql_types::Nullable<db_setup::diesel::schema::sql_types::CustomerService>>>>, Pg>`
     = note: 1 redundant requirement hidden
     = note: required for `Vec<CustomerServiceEnum>` to implement `ToSql<diesel::sql_types::Array<diesel::sql_types::Nullable<diesel::sql_types::Array<diesel::sql_types::Nullable<db_setup::diesel::schema::sql_types::CustomerService>>>>, Pg>`
     = note: required for `Many<diesel::sql_types::Nullable<diesel::sql_types::Array<diesel::sql_types::Nullable<db_setup::diesel::schema::sql_types::CustomerService>>>, CustomerServiceEnum>` to implement `QueryFragment<Pg, pg::backend::PgStyleArrayComparison>`
     = note: 4 redundant requirements hidden
     = note: required for `diesel::expression::grouped::Grouped<In<customer_services, Many<diesel::sql_types::Nullable<diesel::sql_types::Array<diesel::sql_types::Nullable<db_setup::diesel::schema::sql_types::CustomerService>>>, CustomerServiceEnum>>>` to implement `QueryFragment<Pg>`
     = note: required for `query_builder::where_clause::BoxedWhereClause<'_, Pg>` to implement `query_builder::where_clause::WhereAnd<diesel::expression::grouped::Grouped<In<customer_services, Many<diesel::sql_types::Nullable<diesel::sql_types::Array<diesel::sql_types::Nullable<db_setup::diesel::schema::sql_types::CustomerService>>>, CustomerServiceEnum>>>>`
     = note: required for `BoxedSelectStatement<'_, (Uuid, Integer, Jsonb, Text, Nullable<Text>, Nullable<Text>, ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ...), ..., ...>` to implement `FilterDsl<diesel::expression::grouped::Grouped<In<customer_services, Many<diesel::sql_types::Nullable<diesel::sql_types::Array<diesel::sql_types::Nullable<db_setup::diesel::schema::sql_types::CustomerService>>>, CustomerServiceEnum>>>>`
     = note: the full type name has been written to '/home/project_v1/target/debug/deps/database-520d3052fb489996.long-type-4311282295673555358.txt'

error[E0277]: the trait bound `CustomerServiceEnum: SelectableExpression<customers>` is not satisfied
    --> database/src/customer/db/customer/repo.rs:1244:69
     |
1244 | ...                   data_query_total = data_query_total.filter(dsl::customer::props::customer_services.eq_any(filter_customer_services));
     |                                                           ^^^^^^ the trait `SelectableExpression<customers>` is not implemented for `CustomerServiceEnum`
     |
     = help: the following other types implement trait `SelectableExpression<QS>`:
               <Box<T> as SelectableExpression<QS>>
               <diesel::expression::ops::numeric::Add<Lhs, Rhs> as SelectableExpression<QS>>
               <diesel::expression::ops::numeric::Sub<Lhs, Rhs> as SelectableExpression<QS>>
               <diesel::expression::ops::numeric::Mul<Lhs, Rhs> as SelectableExpression<QS>>
               <diesel::expression::ops::numeric::Div<Lhs, Rhs> as SelectableExpression<QS>>
               <now as SelectableExpression<QS>>
               <today as SelectableExpression<QS>>
               <In<T, U> as SelectableExpression<QS>>
             and 16330 others
     = note: required for `Many<diesel::sql_types::Nullable<diesel::sql_types::Array<diesel::sql_types::Nullable<db_setup::diesel::schema::sql_types::CustomerService>>>, CustomerServiceEnum>` to implement `AppearsOnTable<customers>`
     = note: 2 redundant requirements hidden
     = note: required for `diesel::expression::grouped::Grouped<In<customer_services, Many<diesel::sql_types::Nullable<diesel::sql_types::Array<diesel::sql_types::Nullable<db_setup::diesel::schema::sql_types::CustomerService>>>, CustomerServiceEnum>>>` to implement `AppearsOnTable<customers>`
     = note: required for `BoxedSelectStatement<'_, (Uuid, Integer, Jsonb, Text, Nullable<Text>, Nullable<Text>, ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ...), ..., ...>` to implement `FilterDsl<diesel::expression::grouped::Grouped<In<customer_services, Many<diesel::sql_types::Nullable<diesel::sql_types::Array<diesel::sql_types::Nullable<db_setup::diesel::schema::sql_types::CustomerService>>>, CustomerServiceEnum>>>>`
     = note: the full type name has been written to '/home/project_v1/target/debug/deps/database-520d3052fb489996.long-type-13891922749472837152.txt'

error[E0277]: the trait bound `CustomerServiceEnum: ToSql<diesel::sql_types::Nullable<diesel::sql_types::Array<diesel::sql_types::Nullable<db_setup::diesel::schema::sql_types::CustomerService>>>, Pg>` is not satisfied
    --> database/src/customer/db/customer/repo.rs:1244:69
     |
1244 | ...                   data_query_total = data_query_total.filter(dsl::customer::props::customer_services.eq_any(filter_customer_services));
     |                                                           ^^^^^^ the trait `ToSql<diesel::sql_types::Nullable<diesel::sql_types::Array<diesel::sql_types::Nullable<db_setup::diesel::schema::sql_types::CustomerService>>>, Pg>` is not implemented for `CustomerServiceEnum`
     |
     = help: the following other types implement trait `ToSql<A, DB>`:
               <CustomerServiceEnum as ToSql<db_setup::diesel::schema::sql_types::CustomerService, Pg>>
               <CustomerServiceEnum as ToSql<diesel::sql_types::Nullable<db_setup::diesel::schema::sql_types::CustomerService>, DB>>
     = note: required for `[CustomerServiceEnum]` to implement `ToSql<diesel::sql_types::Array<diesel::sql_types::Nullable<diesel::sql_types::Array<diesel::sql_types::Nullable<db_setup::diesel::schema::sql_types::CustomerService>>>>, Pg>`
     = note: 1 redundant requirement hidden
     = note: required for `Vec<CustomerServiceEnum>` to implement `ToSql<diesel::sql_types::Array<diesel::sql_types::Nullable<diesel::sql_types::Array<diesel::sql_types::Nullable<db_setup::diesel::schema::sql_types::CustomerService>>>>, Pg>`
     = note: required for `Many<diesel::sql_types::Nullable<diesel::sql_types::Array<diesel::sql_types::Nullable<db_setup::diesel::schema::sql_types::CustomerService>>>, CustomerServiceEnum>` to implement `QueryFragment<Pg, pg::backend::PgStyleArrayComparison>`
     = note: 4 redundant requirements hidden
     = note: required for `diesel::expression::grouped::Grouped<In<customer_services, Many<diesel::sql_types::Nullable<diesel::sql_types::Array<diesel::sql_types::Nullable<db_setup::diesel::schema::sql_types::CustomerService>>>, CustomerServiceEnum>>>` to implement `QueryFragment<Pg>`
     = note: required for `query_builder::where_clause::BoxedWhereClause<'_, Pg>` to implement `query_builder::where_clause::WhereAnd<diesel::expression::grouped::Grouped<In<customer_services, Many<diesel::sql_types::Nullable<diesel::sql_types::Array<diesel::sql_types::Nullable<db_setup::diesel::schema::sql_types::CustomerService>>>, CustomerServiceEnum>>>>`
     = note: required for `BoxedSelectStatement<'_, (Uuid, Integer, Jsonb, Text, Nullable<Text>, Nullable<Text>, ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ..., ...), ..., ...>` to implement `FilterDsl<diesel::expression::grouped::Grouped<In<customer_services, Many<diesel::sql_types::Nullable<diesel::sql_types::Array<diesel::sql_types::Nullable<db_setup::diesel::schema::sql_types::CustomerService>>>, CustomerServiceEnum>>>>`

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.