Skip to content

Commit

Permalink
Revert Adding Rename Attribute For Column Names SeaQL#2160
Browse files Browse the repository at this point in the history
  • Loading branch information
anshap1719 committed Apr 26, 2024
1 parent 35bca74 commit 4bec02d
Show file tree
Hide file tree
Showing 18 changed files with 51 additions and 63 deletions.
2 changes: 1 addition & 1 deletion issues/1599/entity/src/cake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use sea_orm::entity::prelude::*;
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
#[sea_orm(rename = "name", enum_name = "Name")]
#[sea_orm(column_name = "name", enum_name = "Name")]
pub name: String,
}

Expand Down
10 changes: 5 additions & 5 deletions issues/630/src/entity/underscores_workaround.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ use serde::{Deserialize, Serialize};
pub struct Model {
#[sea_orm(primary_key)]
pub id: u32,
#[sea_orm(rename = "a_b_c_d")]
#[sea_orm(column_name = "a_b_c_d")]
pub a_b_c_d: i32,
#[sea_orm(rename = "a_b_c_dd")]
#[sea_orm(column_name = "a_b_c_dd")]
pub a_b_c_dd: i32,
#[sea_orm(rename = "a_b_cc_d")]
#[sea_orm(column_name = "a_b_cc_d")]
pub a_b_cc_d: i32,
#[sea_orm(rename = "a_bb_c_d")]
#[sea_orm(column_name = "a_bb_c_d")]
pub a_bb_c_d: i32,
#[sea_orm(rename = "aa_b_c_d")]
#[sea_orm(column_name = "aa_b_c_d")]
pub aa_b_c_d: i32,
}

Expand Down
2 changes: 1 addition & 1 deletion issues/693/src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub mod model {
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "container")]
pub struct Model {
#[sea_orm(primary_key, rename = "db_id")]
#[sea_orm(primary_key, column_name = "db_id")]
pub rust_id: i32,
}

Expand Down
8 changes: 4 additions & 4 deletions sea-orm-codegen/src/entity/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,9 +532,9 @@ impl EntityWriter {
let variant = col.get_name_camel_case();
let mut variant = quote! { #variant };
if !col.is_snake_case_name() {
let rename = &col.name;
let column_name = &col.name;
variant = quote! {
#[sea_orm(rename = #rename)]
#[sea_orm(column_name = #column_name)]
#variant
};
}
Expand Down Expand Up @@ -739,8 +739,8 @@ impl EntityWriter {
let mut attrs: Punctuated<_, Comma> = Punctuated::new();
let is_primary_key = primary_keys.contains(&col.name);
if !col.is_snake_case_name() {
let rename = &col.name;
attrs.push(quote! { rename = #rename });
let column_name = &col.name;
attrs.push(quote! { column_name = #column_name });
}
if is_primary_key {
attrs.push(quote! { primary_key });
Expand Down
4 changes: 2 additions & 2 deletions sea-orm-codegen/tests/compact/vendor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use sea_orm::entity::prelude::*;
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
#[sea_orm(rename = "_name_")]
#[sea_orm(column_name = "_name_")]
pub name: String,
#[sea_orm(rename = "fruitId")]
#[sea_orm(column_name = "fruitId")]
pub fruit_id: Option<i32>,
}

Expand Down
4 changes: 2 additions & 2 deletions sea-orm-codegen/tests/compact_with_schema_name/vendor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use sea_orm::entity::prelude::*;
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
#[sea_orm(rename = "_name_")]
#[sea_orm(column_name = "_name_")]
pub name: String,
#[sea_orm(rename = "fruitId")]
#[sea_orm(column_name = "fruitId")]
pub fruit_id: Option<i32>,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use serde::Serialize;
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
#[sea_orm(rename = "_name", column_type = "Text", nullable)]
#[sea_orm(column_name = "_name", column_type = "Text", nullable)]
#[serde(skip)]
pub name: Option<String>,
}
Expand Down
4 changes: 2 additions & 2 deletions sea-orm-codegen/tests/expanded/vendor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ pub struct Model {
#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
pub enum Column {
Id,
#[sea_orm(rename = "_name_")]
#[sea_orm(column_name = "_name_")]
Name,
#[sea_orm(rename = "fruitId")]
#[sea_orm(column_name = "fruitId")]
FruitId,
}

Expand Down
4 changes: 2 additions & 2 deletions sea-orm-codegen/tests/expanded_with_schema_name/vendor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ pub struct Model {
#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
pub enum Column {
Id,
#[sea_orm(rename = "_name_")]
#[sea_orm(column_name = "_name_")]
Name,
#[sea_orm(rename = "fruitId")]
#[sea_orm(column_name = "fruitId")]
FruitId,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct Model {
#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
pub enum Column {
Id,
#[sea_orm(rename = "_name")]
#[sea_orm(column_name = "_name")]
Name,
}

Expand Down
8 changes: 4 additions & 4 deletions sea-orm-macros/src/derives/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ pub fn impl_default_as_str(ident: &Ident, data: &Data) -> syn::Result<TokenStrea
let name: Vec<TokenStream> = variants
.iter()
.map(|v| {
let mut rename = v.ident.to_string().to_snake_case();
let mut column_name = v.ident.to_string().to_snake_case();
for attr in v.attrs.iter() {
if !attr.path().is_ident("sea_orm") {
continue;
}
attr.parse_nested_meta(|meta| {
if meta.path.is_ident("column_name") || meta.path.is_ident("rename") {
rename = meta.value()?.parse::<LitStr>()?.value();
if meta.path.is_ident("column_name") {
column_name = meta.value()?.parse::<LitStr>()?.value();
} else {
// Reads the value expression to advance the parse stream.
// Some parameters, such as `primary_key`, do not have any value,
Expand All @@ -43,7 +43,7 @@ pub fn impl_default_as_str(ident: &Ident, data: &Data) -> syn::Result<TokenStrea
Ok(())
})?;
}
Ok::<TokenStream, syn::Error>(quote! { #rename })
Ok::<TokenStream, syn::Error>(quote! { #column_name })
})
.collect::<Result<_, _>>()?;

Expand Down
24 changes: 6 additions & 18 deletions sea-orm-macros/src/derives/entity_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
let mut ignore = false;
let mut unique = false;
let mut sql_type = None;
let mut rename_attr_present = false;
let mut rename = if let Some(case_style) = rename_all {
let mut column_name = if let Some(case_style) = rename_all {
Some(field_name.convert_case(Some(case_style)))
} else if original_field_name
!= original_field_name.to_upper_camel_case().to_snake_case()
Expand All @@ -128,7 +127,7 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res

let mut enum_name = None;
let mut is_primary_key = false;
// search for #[sea_orm(primary_key, auto_increment = false, column_type = "String(Some(255))", default_value = "new user", default_expr = "gen_random_uuid()", rename = "name", enum_name = "Name", nullable, indexed, unique)]
// search for #[sea_orm(primary_key, auto_increment = false, column_type = "String(Some(255))", default_value = "new user", default_expr = "gen_random_uuid()", column_name = "name", enum_name = "Name", nullable, indexed, unique)]
for attr in field.attrs.iter() {
if !attr.path().is_ident("sea_orm") {
continue;
Expand Down Expand Up @@ -172,23 +171,12 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
} else if meta.path.is_ident("column_name") {
let lit = meta.value()?.parse()?;
if let Lit::Str(litstr) = lit {
rename_attr_present = true;
rename = Some(litstr.value());
column_name = Some(litstr.value());
} else {
return Err(
meta.error(format!("Invalid column_name {:?}", lit))
);
}
} else if meta.path.is_ident("rename") {
let lit = meta.value()?.parse()?;
if let Lit::Str(litstr) = lit {
rename_attr_present = true;
rename = Some(litstr.value());
} else {
return Err(
meta.error(format!("Invalid value for rename {:?}", lit))
);
}
} else if meta.path.is_ident("enum_name") {
let lit = meta.value()?.parse()?;
if let Lit::Str(litstr) = lit {
Expand Down Expand Up @@ -239,9 +227,9 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res

field_name = Ident::new(&escape_rust_keyword(field_name), Span::call_site());

let variant_attrs = match &rename {
Some(rename) => quote! {
#[sea_orm(rename = #rename)]
let variant_attrs = match &column_name {
Some(column_name) => quote! {
#[sea_orm(column_name = #column_name)]
#[doc = " Generated by sea-orm-macros"]
},
None => quote! {
Expand Down
8 changes: 4 additions & 4 deletions sea-orm-macros/src/derives/primary_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ pub fn expand_derive_primary_key(ident: Ident, data: Data) -> syn::Result<TokenS
let name: Vec<TokenStream> = variants
.iter()
.map(|v| {
let mut rename = v.ident.to_string().to_snake_case();
let mut column_name = v.ident.to_string().to_snake_case();
for attr in v.attrs.iter() {
if !attr.path().is_ident("sea_orm") {
continue;
}

attr.parse_nested_meta(|meta| {
if meta.path.is_ident("column_name") || meta.path.is_ident("rename") {
rename = meta.value()?.parse::<LitStr>()?.value();
if meta.path.is_ident("column_name") {
column_name = meta.value()?.parse::<LitStr>()?.value();
} else {
// Reads the value expression to advance the parse stream.
// Some parameters, such as `primary_key`, do not have any value,
Expand All @@ -51,7 +51,7 @@ pub fn expand_derive_primary_key(ident: Ident, data: Data) -> syn::Result<TokenS
Ok(())
})?;
}
Ok::<TokenStream, syn::Error>(quote! { #rename })
Ok::<TokenStream, syn::Error>(quote! { #column_name })
})
.collect::<Result<_, _>>()?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct Model {
username: String,
first_name: String,
middle_name: String,
#[sea_orm(rename = "lAsTnAmE")]
#[sea_orm(column_name = "lAsTnAmE")]
last_name: String,
orders_count: i32,
}
Expand Down
24 changes: 12 additions & 12 deletions src/entity/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -654,10 +654,10 @@ mod tests {
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
#[sea_orm(rename = "ONE")]
#[sea_orm(column_name = "ONE")]
pub one: i32,
pub two: i32,
#[sea_orm(rename = "3")]
#[sea_orm(column_name = "3")]
pub three: i32,
}

Expand Down Expand Up @@ -701,10 +701,10 @@ mod tests {
#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
pub enum Column {
Id,
#[sea_orm(rename = "ONE")]
#[sea_orm(column_name = "ONE")]
One,
Two,
#[sea_orm(rename = "3")]
#[sea_orm(column_name = "3")]
Three,
}

Expand Down Expand Up @@ -863,12 +863,12 @@ mod tests {
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "hello")]
pub struct Model {
#[sea_orm(primary_key, rename = "ID", enum_name = "IdentityColumn")]
#[sea_orm(primary_key, column_name = "ID", enum_name = "IdentityColumn")]
pub id: i32,
#[sea_orm(rename = "ONE", enum_name = "One1")]
#[sea_orm(column_name = "ONE", enum_name = "One1")]
pub one: i32,
pub two: i32,
#[sea_orm(rename = "THREE", enum_name = "Three3")]
#[sea_orm(column_name = "THREE", enum_name = "Three3")]
pub three: i32,
}

Expand Down Expand Up @@ -915,12 +915,12 @@ mod tests {

#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
pub enum Column {
#[sea_orm(rename = "ID")]
#[sea_orm(column_name = "ID")]
IdentityCol,
#[sea_orm(rename = "ONE")]
#[sea_orm(column_name = "ONE")]
One1,
Two,
#[sea_orm(rename = "THREE")]
#[sea_orm(column_name = "THREE")]
Three3,
}

Expand Down Expand Up @@ -974,9 +974,9 @@ mod tests {
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "my_entity")]
pub struct Model {
#[sea_orm(primary_key, enum_name = "IdentityColumn", rename = "id")]
#[sea_orm(primary_key, enum_name = "IdentityColumn", column_name = "id")]
pub id: i32,
#[sea_orm(rename = "type")]
#[sea_orm(column_name = "type")]
pub type_: String,
}

Expand Down
2 changes: 1 addition & 1 deletion src/tests_cfg/cake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use serde::{Deserialize, Serialize};
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
#[sea_orm(rename = "name", enum_name = "Name")]
#[sea_orm(column_name = "name", enum_name = "Name")]
pub name: String,
}

Expand Down
2 changes: 1 addition & 1 deletion src/tests_cfg/cake_seaography.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::entity::prelude::*;
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
#[sea_orm(rename = "name", enum_name = "Name")]
#[sea_orm(column_name = "name", enum_name = "Name")]
pub name: String,
}

Expand Down
2 changes: 1 addition & 1 deletion tests/common/features/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use sea_orm::entity::prelude::*;
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub uuid: Uuid,
#[sea_orm(rename = "type", enum_name = "Type")]
#[sea_orm(column_name = "type", enum_name = "Type")]
pub ty: String,
pub key: String,
pub value: String,
Expand Down

0 comments on commit 4bec02d

Please sign in to comment.