Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@billy1624
Copy link
Member

Resolve #43

@billy1624 billy1624 self-assigned this Aug 12, 2021
@tyt2y3 tyt2y3 merged commit 8b30ef7 into master Aug 12, 2021
@tyt2y3 tyt2y3 deleted the custom-column-name branch August 12, 2021 14:53
@tyt2y3
Copy link
Member

tyt2y3 commented Aug 12, 2021

I insist that after we released a new version of sea-orm-macro, we bump the dependency and remove the path = from toml to ensure that our CI uses the released version.

@tqwewe
Copy link
Contributor

tqwewe commented Aug 12, 2021

I insist that after we released a new version of sea-orm-macro, we bump the dependency and remove the path = from toml to ensure that our CI uses the released version.

I noticed that it used path also, wasn't sure if it was a mistake or not.

This is awesome, you guys work so fast. Is there any examples of how to use this feature to rename columns?

@tyt2y3
Copy link
Member

tyt2y3 commented Aug 12, 2021

// If your column names are not in snake-case, derive `DeriveCustomColumn` here.
#[derive(Copy, Clone, Debug, EnumIter, DeriveCustomColumn)]
pub enum Column {
Id,
Name,
}
// Then, customize each column names here.
impl IdenStatic for Column {
fn as_str(&self) -> &str {
match self {
Self::Id => "id",
Self::Name => "name",
}
}
}

@tqwewe
Copy link
Contributor

tqwewe commented Aug 12, 2021

Oh I see, didn't go with an attribute? Like:

#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)] 
 pub enum Column {
     #[sea(rename = "id")]
     Id,
      #[sea(rename = "full_name")]
     Name, 
 } 

@billy1624
Copy link
Member Author

We want to keep it simple and avoid using excessive macros loll

@tyt2y3
Copy link
Member

tyt2y3 commented Aug 12, 2021

Though, I think we can also have fallback:

 impl IdenStatic for Column { 
     // the default impl is self.default_as_str()
     fn as_str(&self) -> &str { 
         match self { 
             Self::Id => "_id", 
             _ => self.default_as_str(), 
         } 
     } 
 } 

@tqwewe
Copy link
Contributor

tqwewe commented Aug 12, 2021

Ohh okay I see 😄 Serde uses a macro for renaming, but I guess the default_as_str() approach works fine too.. just a little bit more boiletplate code

@tyt2y3
Copy link
Member

tyt2y3 commented Aug 12, 2021

SeaORM tries to avoid custom macro syntax. I think it was our principle to make things runtime-configurable.

@tqwewe
Copy link
Contributor

tqwewe commented Aug 13, 2021

Does the renaming also need to be implemented for DeriveModel? I'm not sure how to handle a model with fields which need to be renamed.

For example, how do we rename columns here:

#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
pub struct Model {
pub id: i32,
pub name: String,
}

@billy1624
Copy link
Member Author

billy1624 commented Aug 13, 2021

Our assumption on naming model attributes & column enum variants:

  • All model attributes are in snake-case and exists in column enum variants
    • Model attributes: id, name
  • All column variants are in camel-case and exists in model attributes
    • Enum variants: Id, Name

So, that means if you want to rename one of the attribute / column you have to also update the other half of it.

@tqwewe
Copy link
Contributor

tqwewe commented Aug 13, 2021

I'm still a little confused, for the column you would rename it with DeriveCustomColumn and impl IdenStatic for Column.. but how do we rename a field on the model too? For exampe, if a field conflicts with a Rust keyword.

@billy1624
Copy link
Member Author

So basically the impl IdenStatic for Column is just for constructing SQL statement, i.e. how we refer this column in SQL statement.

Take your example in #76... we might do the followings

#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
pub struct Model {
    pub id: i32,
    pub type_of: String,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveCustomColumn)]
pub enum Column {
    Id,
    TypeOf,
}

impl IdenStatic for Column {
    fn as_str(&self) -> &str {
        match self {
            Self::TypeOf => "type",
            _ => self.default_as_str(),
        }
    }
}

@tqwewe
Copy link
Contributor

tqwewe commented Aug 13, 2021

Ah perfect, it all makes sense now thank you @billy1624 ... again XD !!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support non snake-case column name

3 participants