-
For an enum field of a diesel::table! {
foos(id) {
id -> Integer,
baz -> Text,
}
}
#[derive(diesel::Queryable, diesel::Selectable)]
pub struct Foo {
baz: Baz,
}
#[derive(
Debug,
diesel::expression::AsExpression,
diesel::deserialize::FromSqlRow,
)]
#[diesel(sql_type = diesel::sql_types::Text)]
pub enum Baz {
Baz1,
Baz2,
}
impl<DB> diesel::deserialize::FromSql<diesel::sql_types::Text, DB> for Baz
where
DB: diesel::backend::Backend,
String: diesel::deserialize::FromSql<diesel::sql_types::Text, DB>,
{
fn from_sql(bytes: DB::RawValue<'_>) -> diesel::deserialize::Result<Self> {
let enum_variant = String::from_sql(bytes)?;
match enum_variant.as_str() {
"Baz1" => Ok(Self::Baz1),
"Baz2" => Ok(Self::Baz2),
_ => panic!(),
}
}
}
impl<DB> diesel::serialize::ToSql<diesel::sql_types::Text, DB> for Baz
where
DB: diesel::backend::Backend,
str: diesel::serialize::ToSql<diesel::sql_types::Text, DB>,
{
fn to_sql<'b>(
&'b self,
out: &mut diesel::serialize::Output<'b, '_, DB>,
) -> diesel::serialize::Result {
match self {
Self::Baz1 => "Baz1".to_sql(out),
Self::Baz2 => "Baz2".to_sql(out),
}
}
}
impl<DB> diesel::expression::Selectable<DB> for Baz
where
DB: diesel::backend::Backend,
{
type SelectExpression =
<Self as diesel::expression::AsExpression<diesel::sql_types::Text>>::Expression;
fn construct_selection() -> Self::SelectExpression {
diesel::expression::AsExpression::<diesel::sql_types::Text>::as_expression(Self::Baz1)
}
} However, I am trying to achieve the same thing with the #[derive(diesel::Queryable, diesel::Selectable)]
pub struct Foo {
#[diesel(
select_expression = diesel::expression::AsExpression::<diesel::sql_types::Text>::as_expression(Baz::Baz1),
select_expression_type = <Self as diesel::expression::AsExpression<diesel::sql_types::Text>>::Expression,
)]
baz: Baz,
} I have tried various ways of adapting the expressions (note that |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
It's really hard to answer questions that go like:
If you don't share what you exactly tried and how that did not work it requires a large amount of work from our side to understand what is exactly not working. That usually means you need to wait until someone finds time to do that, if that ever happens. So if you are interested in fast answers you really need to try to include as much information in your question as possible and not end with "I've tried stuff and it did not work" without being specific about what you tried and why it did not work. That usually means you need to at least include the exact error message as emitted by rustc to the code you've posted. |
Beta Was this translation helpful? Give feedback.
Doing the same thing in the
select_expression_type
was the only missing thing, thanks 👍