Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Column name type #194

Closed
cemoktra opened this issue Sep 24, 2021 · 4 comments · Fixed by #209
Closed

Column name type #194

cemoktra opened this issue Sep 24, 2021 · 4 comments · Fixed by #209
Assignees

Comments

@cemoktra
Copy link
Contributor

Hey,

when generating code column named type should be renamed to r#type. Maybe check other rust keywords.

@billy1624
Copy link
Member

Related to #76

@tyt2y3 tyt2y3 added this to the 0.2.x milestone Sep 27, 2021
@tyt2y3
Copy link
Member

tyt2y3 commented Sep 27, 2021

Continuing from #82

Annotations we need to add to DeriveColumn

#[derive(DeriveColumn)]
pub enum Column {
    #[sea_orm(column_name = "my_db_col")]
    Name,
}

And also DeriveEntityModel

#[derive(DeriveEntityModel)]
#[sea_orm(table_name = "cake")]
pub struct Model {
    #[sea_orm(column_type = "Text", column_name = "my_db_col", nullable)]
    pub name: Option<String> ,
}

If feeling fancy, we can also add enum variant remapping:

#[derive(DeriveEntityModel)]
#[sea_orm(table_name = "cake")]
pub struct Model {
    #[sea_orm(column_name = "type", enum_name = "Type")]
    pub type_: String ,
}

which would generate the following:

#[derive(DeriveColumn)]
pub enum Column {
    #[sea_orm(column_name = "type")]
    Type,
}

@DanielHZhang
Copy link

Would it be possible to also include a rename macro such as #[sea_orm(casing = "camelCase")] for DeriveColumn or DeriveEntityModel?

I know it's been discussed previously that SeaORM wants to avoid relying too much on macro magic, but I think it's a common pain point that database column names are in PascalCase or camelCase and currently there's no convenient way to remap them without writing the expanded entity structure.

The macro could look something like this:

#[derive(DeriveEntityModel)]
#[sea_orm(table_name = "Account", casing = "PascalCase")]
pub struct Model {
    #[sea_orm(column_name = "type", enum_name = "Type")]
    pub ty: String,
	pub first_name: String, // mapped to db column `FirstName`
	pub last_name: String, // mapped to db column `LastName`
}

@billy1624
Copy link
Member

Hey @DanielHZhang, good suggestion. It's possible to add this feature.

arpancodes pushed a commit to arpancodes/sea-orm that referenced this issue Apr 8, 2022
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 a pull request may close this issue.

4 participants