diff --git a/Cargo.toml b/Cargo.toml index 27380d04..f73cfa51 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -67,7 +67,6 @@ with-json = ["serde_json", "sea-query/with-json", "chrono/serde"] with-chrono = ["chrono", "sea-query/with-chrono"] with-rust_decimal = ["rust_decimal", "sea-query/with-rust_decimal"] with-uuid = ["uuid", "sea-query/with-uuid"] -with-table-iden = ["sea-orm-macros/with-table-iden"] sqlx-all = ["sqlx-mysql", "sqlx-postgres", "sqlx-sqlite"] sqlx-dep = ["sqlx-json", "sqlx-chrono", "sqlx-decimal", "sqlx-uuid"] sqlx-json = ["sqlx/json", "with-json"] diff --git a/issues/356/Cargo.toml b/issues/356/Cargo.toml index 4e0e64d0..ed64bae4 100644 --- a/issues/356/Cargo.toml +++ b/issues/356/Cargo.toml @@ -8,4 +8,4 @@ edition = "2021" publish = false [dependencies] -sea-orm = { path = "../../", features = [ "sqlx-mysql", "runtime-async-std-native-tls", "with-table-iden" ]} +sea-orm = { path = "../../", features = [ "sqlx-mysql", "runtime-async-std-native-tls" ]} diff --git a/issues/356/src/model.rs b/issues/356/src/model.rs index 44325a3a..912a3dcd 100644 --- a/issues/356/src/model.rs +++ b/issues/356/src/model.rs @@ -1,7 +1,7 @@ use sea_orm::entity::prelude::*; #[derive(Clone, Debug, PartialEq, DeriveEntityModel)] -#[sea_orm(table_name = "model")] +#[sea_orm(table_name = "model", table_iden)] pub struct Model { #[sea_orm(primary_key)] pub id: i32, diff --git a/sea-orm-macros/Cargo.toml b/sea-orm-macros/Cargo.toml index be22fb2f..c4c82756 100644 --- a/sea-orm-macros/Cargo.toml +++ b/sea-orm-macros/Cargo.toml @@ -25,6 +25,3 @@ proc-macro2 = "^1" [dev-dependencies] sea-orm = { path = "../", features = ["macros"] } serde = { version = "^1.0", features = ["derive"] } - -[features] -with-table-iden = [] diff --git a/sea-orm-macros/src/attributes.rs b/sea-orm-macros/src/attributes.rs index 3f479bb9..cb47d029 100644 --- a/sea-orm-macros/src/attributes.rs +++ b/sea-orm-macros/src/attributes.rs @@ -11,6 +11,7 @@ pub mod derive_attr { pub relation: Option, pub schema_name: Option, pub table_name: Option, + pub table_iden: Option<()>, } } diff --git a/sea-orm-macros/src/derives/entity_model.rs b/sea-orm-macros/src/derives/entity_model.rs index e42957a2..7309a6c4 100644 --- a/sea-orm-macros/src/derives/entity_model.rs +++ b/sea-orm-macros/src/derives/entity_model.rs @@ -12,6 +12,7 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec) -> syn::Res // if #[sea_orm(table_name = "foo", schema_name = "bar")] specified, create Entity struct let mut table_name = None; let mut schema_name = quote! { None }; + let mut table_iden = false; attrs.iter().for_each(|attr| { if attr.path.get_ident().map(|i| i == "sea_orm") != Some(true) { return; @@ -28,6 +29,12 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec) -> syn::Res schema_name = quote! { Some(#name) }; } } + } else if let Meta::Path(path) = meta { + if let Some(ident) = path.get_ident() { + if ident == "table_iden" { + table_iden = true; + } + } } } } @@ -59,17 +66,18 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec) -> syn::Res let mut primary_keys: Punctuated<_, Comma> = Punctuated::new(); let mut primary_key_types: Punctuated<_, Comma> = Punctuated::new(); let mut auto_increment = true; - - #[cfg(feature = "with-table-iden")] - if let Some(table_name) = table_name { - let table_field_name = Ident::new("Table", Span::call_site()); - columns_enum.push(quote! { - #[sea_orm(table_name=#table_name)] - #[strum(disabled)] - #table_field_name - }); - columns_trait - .push(quote! { Self::#table_field_name => panic!("Table cannot be used as a column") }); + if table_iden { + if let Some(table_name) = table_name { + let table_field_name = Ident::new("Table", Span::call_site()); + columns_enum.push(quote! { + #[sea_orm(table_name=#table_name)] + #[strum(disabled)] + #table_field_name + }); + columns_trait.push( + quote! { Self::#table_field_name => panic!("Table cannot be used as a column") }, + ); + } } if let Data::Struct(item_struct) = data { if let Fields::Named(fields) = item_struct.fields {