I’m trying to build a Diesel query that will make the search Postgres field defined as a varchar array similar to this SELECT value FROM table WHERE ANY VALUE IN array_column = 'Carol';
But this throws this error.
What I’m doing wrong?
use diesel::{dsl::any, prelude::*};
use dto::{delivery_provider::DeliveryProvider, response_models::{Response, ResponseBody}};
use infrastructure::establish_connection;
use rocket::response::status::NotFound;
pub fn get_providers_by_country(
country_code: &str,
) -> Result<Vec<DeliveryProvider>, NotFound<String>> {
use infrastructure::schema::delivery_providers::dsl::*;
let providers = delivery_providers
.select(DeliveryProvider::as_select())
.filter(country_operate.eq(any(vec![country_code])))
.load::<DeliveryProvider>(&mut establish_connection());
match providers {
Ok(providers) => Ok(providers),
Err(err) => match err {
diesel::result::Error::NotFound => {
let response = Response {
body: ResponseBody::Message(format!("Error selecting delivery provider - {}", err)),
};
return Err(NotFound(serde_json::to_string(&response).unwrap()));
}
_ => {
panic!("Database error - {}", err);
}
},
}
}
Diesel schema:
delivery_providers (id) {
id -> Int4,
provider_name -> Varchar,
country_operate -> Array<Varchar>,
api_address -> Nullable<Varchar>,
api_key -> Nullable<Varchar>,
api_password -> Nullable<Varchar>,
support_email -> Nullable<Varchar>,
comments -> Nullable<Varchar>,
}
Database table:
id SERIAL PRIMARY KEY,
provider_name VARCHAR NOT NULL,
country_operate TEXT[] NOT NULL,
api_address VARCHAR NULL,
api_key VARCHAR NULL,
api_password VARCHAR NULL,
support_email VARCHAR NULL,
comments VARCHAR NULL
cargo check output
error[E0277]: the trait bound `str: ToSql<diesel::sql_types::Array<diesel::sql_types::Text>, Pg>` is not satisfied
--> applicationsrcdelivery_providerread.rs:14:35
|
14 | .load::<DeliveryProvider>(&mut establish_connection());
| ---- ^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `ToSql<diesel::sql_types::Array<diesel::sql_types::Text>, Pg>` is not implemented for `str`, which is required by `SelectStatement<FromClause<infrastructure::schema::delivery_providers::table>, query_builder::select_clause::SelectClause<diesel::expression::select_by::SelectBy<dto::delivery_provider::DeliveryProvider, _>>, query_builder::distinct_clause::NoDistinctClause, query_builder::where_clause::WhereClause<diesel::expression::grouped::Grouped<diesel::expression::operators::Eq<infrastructure::schema::delivery_providers::country_operate, diesel::pg::expression::array_comparison::Any<diesel::expression::bound::Bound<diesel::sql_types::Array<diesel::sql_types::Array<diesel::sql_types::Text>>, Vec<&str>>>>>>>: LoadQuery<'_, _, dto::delivery_provider::DeliveryProvider>`
| |
| required by a bound introduced by this call
|
= help: the following other types implement trait `ToSql<A, DB>`:
<str as ToSql<Citext, Pg>>
<str as ToSql<Nullable<Citext>, __DB>>
<str as ToSql<Nullable<diesel::sql_types::Text>, __DB>>
<str as ToSql<diesel::sql_types::Text, DB>>
= note: required for `&str` to implement `ToSql<diesel::sql_types::Array<diesel::sql_types::Text>, Pg>`
= note: 2 redundant requirements hidden
= note: required for `Vec<&str>` to implement `ToSql<diesel::sql_types::Array<diesel::sql_types::Array<diesel::sql_types::Text>>, Pg>`
= note: required for `diesel::expression::bound::Bound<diesel::sql_types::Array<diesel::sql_types::Array<diesel::sql_types::Text>>, Vec<&str>>` to implement `QueryFragment<Pg>`
= note: 9 redundant requirements hidden
= note: required for `SelectStatement<FromClause<table>, SelectClause<SelectBy<DeliveryProvider, Pg>>, NoDistinctClause, WhereClause<Grouped<Eq<country_operate, Any<Bound<Array<Array<Text>>, Vec<&str>>>>>>>` to implement `QueryFragment<Pg>`
= note: required for `SelectStatement<FromClause<table>, SelectClause<SelectBy<DeliveryProvider, Pg>>, NoDistinctClause, WhereClause<Grouped<Eq<country_operate, Any<Bound<Array<Array<Text>>, Vec<&str>>>>>>>` to implement `LoadQuery<'_, _, dto::delivery_provider::DeliveryProvider>`
note: required by a bound in `diesel::RunQueryDsl::load`
--> C:Usersandre.cargoregistrysrcindex.crates.io-6f17d22bba15001fdiesel-2.2.1srcquery_dslmod.rs:1542:15
|
1540 | fn load<'query, U>(self, conn: &mut Conn) -> QueryResult<Vec<U>>
| ---- required by a bound in this associated function
1541 | where
1542 | Self: LoadQuery<'query, Conn, U>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `RunQueryDsl::load`
= note: the full name for the type has been written to 'C:UsersandreRepositoriesintegrationstargetdebugdepsapplication-4e5463df3b187922.long-type-10055166271310308511.txt'
= note: consider using `--verbose` to print the full type name to the console
= note: the full name for the type has been written to 'C:UsersandreRepositoriesintegrationstargetdebugdepsapplication-4e5463df3b187922.long-type-10055166271310308511.txt'
= note: consider using `--verbose` to print the full type name to the console
error[E0271]: type mismatch resolving `<Pg as SqlDialect>::SelectStatementSyntax == NotSpecialized`
--> applicationsrcdelivery_providerread.rs:14:35
|
14 | .load::<DeliveryProvider>(&mut establish_connection());
| ---- ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `NotSpecialized`, found `AnsiSqlSelectStatement`
| |
| required by a bound introduced by this call
|
= note: required for `SelectStatement<FromClause<table>, SelectClause<SelectBy<DeliveryProvider, Pg>>, NoDistinctClause, WhereClause<Grouped<Eq<country_operate, Any<Bound<Array<Array<Text>>, Vec<&str>>>>>>>` to implement `QueryFragment<Pg>`
= note: required for `SelectStatement<FromClause<table>, SelectClause<SelectBy<DeliveryProvider, Pg>>, NoDistinctClause, WhereClause<Grouped<Eq<country_operate, Any<Bound<Array<Array<Text>>, Vec<&str>>>>>>>` to implement `LoadQuery<'_, _, dto::delivery_provider::DeliveryProvider>`
note: required by a bound in `diesel::RunQueryDsl::load`
--> C:Usersandre.cargoregistrysrcindex.crates.io-6f17d22bba15001fdiesel-2.2.1srcquery_dslmod.rs:1542:15
|
1540 | fn load<'query, U>(self, conn: &mut Conn) -> QueryResult<Vec<U>>
| ---- required by a bound in this associated function
1541 | where
1542 | Self: LoadQuery<'query, Conn, U>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `RunQueryDsl::load`
= note: the full name for the type has been written to 'C:UsersandreRepositoriesintegrationstargetdebugdepsapplication-4e5463df3b187922.long-type-10055166271310308511.txt'
= note: consider using `--verbose` to print the full type name to the console
= note: the full name for the type has been written to 'C:UsersandreRepositoriesintegrationstargetdebugdepsapplication-4e5463df3b187922.long-type-10055166271310308511.txt'
= note: consider using `--verbose` to print the full type name to the console
3