diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 40c20d01..e98024b9 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -314,7 +314,7 @@ jobs: strategy: matrix: os: [ubuntu-latest] - path: [86, 249, 262, 319] + path: [86, 249, 262, 319, 324] steps: - uses: actions/checkout@v2 diff --git a/issues/324/Cargo.toml b/issues/324/Cargo.toml new file mode 100644 index 00000000..9effe6d2 --- /dev/null +++ b/issues/324/Cargo.toml @@ -0,0 +1,11 @@ +[workspace] +# A separate workspace + +[package] +name = "sea-orm-issues-324" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] +sea-orm = { path = "../../", features = [ "sqlx-mysql", "runtime-async-std-native-tls" ]} diff --git a/issues/324/src/main.rs b/issues/324/src/main.rs new file mode 100644 index 00000000..a9db43c3 --- /dev/null +++ b/issues/324/src/main.rs @@ -0,0 +1,3 @@ +mod model; + +pub fn main() {} diff --git a/issues/324/src/model.rs b/issues/324/src/model.rs new file mode 100644 index 00000000..9ec35a07 --- /dev/null +++ b/issues/324/src/model.rs @@ -0,0 +1,83 @@ +use sea_orm::entity::prelude::*; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel)] +#[sea_orm(table_name = "model")] +pub struct Model { + #[sea_orm(primary_key)] + pub id: AccountId, + pub name: String, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation {} + +impl ActiveModelBehavior for ActiveModel {} + +#[derive(Clone, Debug, PartialEq)] +pub struct AccountId(Uuid); + +impl From for Uuid { + fn from(account_id: AccountId) -> Self { + account_id.0 + } +} + +macro_rules! impl_try_from_u64_err { + ($newtype: ident) => { + impl sea_orm::TryFromU64 for $newtype { + fn try_from_u64(_n: u64) -> Result { + Err(sea_orm::DbErr::Exec(format!( + "{} cannot be converted from u64", + stringify!($newtype) + ))) + } + } + }; +} + +macro_rules! into_sea_query_value { + ($newtype: ident: Box($name: ident)) => { + impl From<$newtype> for sea_orm::Value { + fn from(source: $newtype) -> Self { + sea_orm::Value::$name(Some(Box::new(source.into()))) + } + } + + impl sea_orm::TryGetable for $newtype { + fn try_get( + res: &sea_orm::QueryResult, + pre: &str, + col: &str, + ) -> Result { + let val: $name = res.try_get(pre, col).map_err(sea_orm::TryGetError::DbErr)?; + Ok($newtype(val)) + } + } + + impl sea_orm::sea_query::Nullable for $newtype { + fn null() -> sea_orm::Value { + sea_orm::Value::$name(None) + } + } + + impl sea_orm::sea_query::ValueType for $newtype { + fn try_from(v: sea_orm::Value) -> Result { + match v { + sea_orm::Value::$name(Some(x)) => Ok($newtype(*x)), + _ => Err(sea_orm::sea_query::ValueTypeErr), + } + } + + fn type_name() -> String { + stringify!($newtype).to_owned() + } + + fn column_type() -> sea_orm::sea_query::ColumnType { + sea_orm::sea_query::ColumnType::$name + } + } + }; +} + +into_sea_query_value!(AccountId: Box(Uuid)); +impl_try_from_u64_err!(AccountId); diff --git a/sea-orm-macros/src/derives/entity_model.rs b/sea-orm-macros/src/derives/entity_model.rs index 5f1508ef..e39fecf1 100644 --- a/sea-orm-macros/src/derives/entity_model.rs +++ b/sea-orm-macros/src/derives/entity_model.rs @@ -237,7 +237,10 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec) -> syn::Res let field_span = field.span(); let ty = format_ident!("{}", temp); let def = quote_spanned! { field_span => { - <#ty as ActiveEnum>::db_type() + std::convert::Into::::into( + <#ty as sea_orm::sea_query::ValueType>::column_type() + ) + .def() }}; quote! { #def } } else {