use std::marker::PhantomData; 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, PhantomData); impl AccountId { pub fn new(id: Uuid) -> Self { AccountId(id, PhantomData) } } impl From> for Uuid { fn from(account_id: AccountId) -> Self { account_id.0 } } impl sea_orm::TryFromU64 for AccountId { fn try_from_u64(_n: u64) -> Result { Err(sea_orm::DbErr::Exec(format!( "{} cannot be converted from u64", stringify!(AccountId) ))) } } impl From> for sea_orm::Value { fn from(source: AccountId) -> Self { sea_orm::Value::Uuid(Some(Box::new(source.into()))) } } impl sea_orm::TryGetable for AccountId { fn try_get( res: &sea_orm::QueryResult, pre: &str, col: &str, ) -> Result { let val: Uuid = res.try_get(pre, col).map_err(sea_orm::TryGetError::DbErr)?; Ok(AccountId::::new(val)) } } impl sea_orm::sea_query::Nullable for AccountId { fn null() -> sea_orm::Value { sea_orm::Value::Uuid(None) } } impl sea_orm::sea_query::ValueType for AccountId { fn try_from(v: sea_orm::Value) -> Result { match v { sea_orm::Value::Uuid(Some(x)) => Ok(AccountId::::new(*x)), _ => Err(sea_orm::sea_query::ValueTypeErr), } } fn type_name() -> String { stringify!(AccountId).to_owned() } fn column_type() -> sea_orm::sea_query::ColumnType { sea_orm::sea_query::ColumnType::Uuid } }