adwhit / diesel-derive-enum

Use enums with Diesel ORM
Apache License 2.0
270 stars 30 forks source link

No implementation of `FromSql`? #78

Closed xoac closed 2 years ago

xoac commented 2 years ago

Hi I am using derive enum as follow:

diesel = { version = "1.4.8", features = ["postgres", "r2d2", "chrono", "serde_json"] }
diesel-derive-enum = "1.1.2"

my definition of TransactionStatus

use diesel_derive_enum::DbEnum;

#[derive(Debug, Clone, PartialEq, Eq, DbEnum)]
#[DbValueStyle = "PascalCase"]
#[PgType = "TransactionStatus"]
#[DieselType = "TransactionStatusMapping"]
pub enum TransactionStatus {
    Sucess,
    Failure,
}

And I got 2 errors:

error[E0277]: the trait bound `Pg: HasSqlType<TransactionStatusMapping>` is not satisfied
    --> src/pg_db/models/transaction.rs:111:10
     |
111  |         .get_results(db_conn)
     |          ^^^^^^^^^^^ the trait `HasSqlType<TransactionStatusMapping>` is not implemented for `Pg`
     |
 error[E0277]: the trait bound `transaction_status::TransactionStatus: Queryable<TransactionStatusMapping, Pg>` is not satisfied
    --> src/pg_db/models/transaction.rs:107:10
     |
107  |         .get_results(db_conn)
     |          ^^^^^^^^^^^ the trait `Queryable<TransactionStatusMapping, Pg>` is not implemented for `transaction_status::TransactionStatus`
     |

So I wanted to understand details and run cargo extend This is output:

  pub mod transaction_status {
    use diesel_derive_enum::DbEnum;
    #[DbValueStyle = "PascalCase"]
    #[PgType = "TransactionStatus"]
    #[DieselType = "TransactionStatusMapping"]
    pub enum TransactionStatus {
        Sucess,
        Failure,
    }
    #[automatically_derived]
    impl ::core::fmt::Debug for TransactionStatus {
        fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
            match self {
                TransactionStatus::Sucess => {
                    ::core::fmt::Formatter::write_str(f, "Sucess")
                }
                TransactionStatus::Failure => {
                    ::core::fmt::Formatter::write_str(f, "Failure")
                }
            }
        }
    }
    #[automatically_derived]
    impl ::core::clone::Clone for TransactionStatus {
        #[inline]
        fn clone(&self) -> TransactionStatus {
            match self {
                TransactionStatus::Sucess => TransactionStatus::Sucess,
                TransactionStatus::Failure => TransactionStatus::Failure,
            }
        }
    }
    impl ::core::marker::StructuralPartialEq for TransactionStatus {}
    #[automatically_derived]
    impl ::core::cmp::PartialEq for TransactionStatus {
        #[inline]
        fn eq(&self, other: &TransactionStatus) -> bool {
            let __self_tag = ::core::intrinsics::discriminant_value(self);
            let __arg1_tag = ::core::intrinsics::discriminant_value(other);
            __self_tag == __arg1_tag
        }
    }
    impl ::core::marker::StructuralEq for TransactionStatus {}
    #[automatically_derived]
    impl ::core::cmp::Eq for TransactionStatus {
        #[inline]
        #[doc(hidden)]
        #[no_coverage]
        fn assert_receiver_is_total_eq(&self) -> () {}
    }
    pub use self::db_enum_impl_TransactionStatus::TransactionStatusMapping;
    #[allow(non_snake_case)]
    mod db_enum_impl_TransactionStatus {
        use super::*;
        use diesel::Queryable;
        use diesel::backend::Backend;
        use diesel::expression::AsExpression;
        use diesel::expression::bound::Bound;
        use diesel::row::Row;
        use diesel::sql_types::*;
        use diesel::serialize::{self, ToSql, IsNull, Output};
        use diesel::deserialize::{self, FromSql, FromSqlRow};
        use diesel::query_builder::QueryId;
        use std::io::Write;
        pub struct TransactionStatusMapping;
        #[automatically_derived]
        impl ::core::clone::Clone for TransactionStatusMapping {
            #[inline]
            fn clone(&self) -> TransactionStatusMapping {
                TransactionStatusMapping
            }
        }
        impl QueryId for TransactionStatusMapping {
            type QueryId = TransactionStatusMapping;
            const HAS_STATIC_QUERY_ID: bool = true;
        }
        impl NotNull for TransactionStatusMapping {}
        impl SingleValue for TransactionStatusMapping {}
        impl AsExpression<TransactionStatusMapping> for TransactionStatus {
            type Expression = Bound<TransactionStatusMapping, Self>;
            fn as_expression(self) -> Self::Expression {
                Bound::new(self)
            }
        }
        impl AsExpression<Nullable<TransactionStatusMapping>> for TransactionStatus {
            type Expression = Bound<Nullable<TransactionStatusMapping>, Self>;
            fn as_expression(self) -> Self::Expression {
                Bound::new(self)
            }
        }
        impl<'a> AsExpression<TransactionStatusMapping> for &'a TransactionStatus {
            type Expression = Bound<TransactionStatusMapping, Self>;
            fn as_expression(self) -> Self::Expression {
                Bound::new(self)
            }
        }
        impl<'a> AsExpression<Nullable<TransactionStatusMapping>>
        for &'a TransactionStatus {
            type Expression = Bound<Nullable<TransactionStatusMapping>, Self>;
            fn as_expression(self) -> Self::Expression {
                Bound::new(self)
            }
        }
        impl<'a, 'b> AsExpression<TransactionStatusMapping>
        for &'a &'b TransactionStatus {
            type Expression = Bound<TransactionStatusMapping, Self>;
            fn as_expression(self) -> Self::Expression {
                Bound::new(self)
            }
        }
        impl<'a, 'b> AsExpression<Nullable<TransactionStatusMapping>>
        for &'a &'b TransactionStatus {
            type Expression = Bound<Nullable<TransactionStatusMapping>, Self>;
            fn as_expression(self) -> Self::Expression {
                Bound::new(self)
            }
        }
        impl<DB: Backend> ToSql<TransactionStatusMapping, DB> for TransactionStatus {
            fn to_sql<W: Write>(&self, out: &mut Output<W, DB>) -> serialize::Result {
                match *self {
                    TransactionStatus::Sucess => out.write_all(b"Sucess")?,
                    TransactionStatus::Failure => out.write_all(b"Failure")?,
                }
                Ok(IsNull::No)
            }
        }
        impl<DB> ToSql<Nullable<TransactionStatusMapping>, DB> for TransactionStatus
        where
            DB: Backend,
            Self: ToSql<TransactionStatusMapping, DB>,
        {
            fn to_sql<W: ::std::io::Write>(
                &self,
                out: &mut Output<W, DB>,
            ) -> serialize::Result {
                ToSql::<TransactionStatusMapping, DB>::to_sql(self, out)
            }
        }
    }
}

And I do not see implementation of FromSql what can be reason why I got this errors?

adwhit commented 2 years ago

You need to activate the postgres feature of the crate.

xoac commented 2 years ago

Thanks for quick answer. I do not know how I missed it from README