diff --git a/sea-orm-macros/src/derives/entity.rs b/sea-orm-macros/src/derives/entity.rs index afad8319..de6b97fb 100644 --- a/sea-orm-macros/src/derives/entity.rs +++ b/sea-orm-macros/src/derives/entity.rs @@ -17,7 +17,7 @@ fn get_entity_attr(attrs: &[Attribute]) -> Option { } pub fn expand_derive_entity(ident: Ident, attrs: Vec) -> syn::Result { - let entity_name = match get_entity_attr(&attrs) { + let _entity_name = match get_entity_attr(&attrs) { Some(lit) => quote! { #lit }, None => { let normalized = ident.to_string().to_snake_case(); @@ -26,20 +26,18 @@ pub fn expand_derive_entity(ident: Ident, attrs: Vec) -> syn::Result< }; Ok(quote!( - impl sea_orm::EntityName for #ident {} - - impl sea_orm::IdenStatic for #ident { - fn as_str(&self) -> &str { - #entity_name - } - } - impl sea_orm::Iden for #ident { fn unquoted(&self, s: &mut dyn std::fmt::Write) { write!(s, "{}", self.as_str()).unwrap(); } } + impl sea_orm::IdenStatic for #ident { + fn as_str(&self) -> &str { + ::table_name(self) + } + } + impl EntityTrait for #ident { type Model = Model; diff --git a/src/entity/base.rs b/src/entity/base.rs index 2de70d83..34b7f7d9 100644 --- a/src/entity/base.rs +++ b/src/entity/base.rs @@ -11,7 +11,13 @@ pub trait IdenStatic: Iden + Copy + Debug + 'static { fn as_str(&self) -> &str; } -pub trait EntityName: IdenStatic + Default {} +pub trait EntityName: IdenStatic + Default { + fn table_name(&self) -> &str; + + fn module_name(&self) -> &str { + Self::table_name(self) + } +} pub trait EntityTrait: EntityName { type Model: ModelTrait + FromQueryResult; diff --git a/src/tests_cfg/cake.rs b/src/tests_cfg/cake.rs index 6b17e1b3..2d3c3de2 100644 --- a/src/tests_cfg/cake.rs +++ b/src/tests_cfg/cake.rs @@ -2,9 +2,14 @@ use crate as sea_orm; use crate::entity::prelude::*; #[derive(Copy, Clone, Default, Debug, DeriveEntity)] -#[table = "cake"] pub struct Entity; +impl EntityName for Entity { + fn table_name(&self) -> &str { + "cake" + } +} + #[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)] pub struct Model { pub id: i32, diff --git a/src/tests_cfg/cake_filling.rs b/src/tests_cfg/cake_filling.rs index 1837d6c8..f5de2391 100644 --- a/src/tests_cfg/cake_filling.rs +++ b/src/tests_cfg/cake_filling.rs @@ -2,9 +2,14 @@ use crate as sea_orm; use crate::entity::prelude::*; #[derive(Copy, Clone, Default, Debug, DeriveEntity)] -#[table = "cake_filling"] pub struct Entity; +impl EntityName for Entity { + fn table_name(&self) -> &str { + "cake_filling" + } +} + #[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)] pub struct Model { pub cake_id: i32, diff --git a/src/tests_cfg/filling.rs b/src/tests_cfg/filling.rs index 856598d5..11efff5b 100644 --- a/src/tests_cfg/filling.rs +++ b/src/tests_cfg/filling.rs @@ -2,9 +2,14 @@ use crate as sea_orm; use crate::entity::prelude::*; #[derive(Copy, Clone, Default, Debug, DeriveEntity)] -#[table = "filling"] pub struct Entity; +impl EntityName for Entity { + fn table_name(&self) -> &str { + "filling" + } +} + #[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)] pub struct Model { pub id: i32, diff --git a/src/tests_cfg/fruit.rs b/src/tests_cfg/fruit.rs index c99433c2..b69f445b 100644 --- a/src/tests_cfg/fruit.rs +++ b/src/tests_cfg/fruit.rs @@ -2,9 +2,14 @@ use crate as sea_orm; use crate::entity::prelude::*; #[derive(Copy, Clone, Default, Debug, DeriveEntity)] -#[table = "fruit"] pub struct Entity; +impl EntityName for Entity { + fn table_name(&self) -> &str { + "fruit" + } +} + #[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)] pub struct Model { pub id: i32,