+
Skip to content

fix: include column_name in DeriveColumn FromStr impl #2603

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

Merged

Conversation

lazulit3
Copy link
Contributor

@lazulit3 lazulit3 commented May 22, 2025

PR Info

I'm pretty sure this fixes the cause of:

I encountered the same error when trying to use load_many()

thread 'test::all_artists' panicked at /REDACTED/sea-orm/src/query/loader.rs:361:21:
Failed at mapping string to column A:1: ID

My generated entities have column names in an (almost) UpperCamelCase style: #[sea_orm(column_name = "UpperCamelCase")] (with some exceptions like ArtistID)

When expanding my DeriveEntityModel and then DeriveColumn, I found that the FromStr implementation did not include my column names:

#[automatically_derived]
impl std::str::FromStr for Column {
    type Err = sea_orm::ColumnFromStrErr;
    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
        match s {
            "id" | "id" => Ok(Column::Id),
            "artist_id" | "artistId" => Ok(Column::ArtistId),
            _ => Err(sea_orm::ColumnFromStrErr(s.to_owned())),
        }
    }
}

This ensures that we include values from the column_name attributes too, like so:

            "id" | "id" | "ID" => Ok(Column::Id),
            "artist_id" | "artistId" | "ArtistID" => Ok(Column::ArtistId),

Bug Fixes

Changes

  • Column::from_str() now recognizes values from #[sea_orm(column_name = "...")] attributes set on:
    • The Column variants (when Column is an expanded definition with DeriveColumn)
    • The Model fields (when DeriveEntityModel is used)

Testing

I added a test case to an already existing test for DeriveEntityModel related to column names. This verifies the specific change (and would identify a regression). The column used has a column_name with weird casing that is not covered by the snake_case or lowerCamelCase styles (that the macro already handled).

More importantly, I verified that this fixed my problems with load_many() in a personal project— which is the symptom that led to the linked issue & discussion.

lazulit3 added 2 commits May 21, 2025 18:17
This updates the `FromStr` implementation in `DeriveColumn` to ensure that `column_name` attributes are recognized.

When custom `column_name`s did not match the snake_case or lowerCamelCase variants (that the derivation already provides), then a column Iden could fail to map to a column.
This adds a test case for `DeriveEntityModel` colum names to verify that `Column::from_str` succeeds on a particularly weird `column_name`.
Comment on lines +81 to +100
let mut column_name = column_str_snake.clone();
for attr in column.attrs.iter() {
if !attr.path().is_ident("sea_orm") {
continue;
}
attr.parse_nested_meta(|meta| {
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,
// so ignoring an error occurred here.
let _: Option<Expr> = meta.value().and_then(|v| v.parse()).ok();
}
Ok(())
})?;
}
Ok::<TokenStream, syn::Error>(quote!(
#column_str_snake | #column_str_mixed | #column_name => Ok(#ident::#column_iden)
))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied the attribute parsing from impl_default_as_str (above in column.rs).

In cases where #[sea_orm(column_name = "...")] is not found, this results in a redundant string match like:

"artist_id" | "artistId" | "artist_id" => Ok(Column::ArtistId),

I recognize that this is not very clean— it would be nicer to only include additional strings when they differ.
(I don't have the patience to think through that, so I invite help if we care about the redundancy. <3)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would be fine as long as it doesnt trigger clippy warning

@lazulit3 lazulit3 changed the title fix: include column_name in DeriveColumn FromStr fix: include column_name in DeriveColumn FromStr impl May 22, 2025
@tyt2y3 tyt2y3 merged commit 58fdac3 into SeaQL:master May 26, 2025
35 of 36 checks passed
Copy link

🎉 Released In 1.1.12 🎉

Thank you everyone for the contribution!
This feature is now available in the latest release. Now is a good time to upgrade!
Your participation is what makes us unique; your adoption is what drives us forward.
You can support SeaQL 🌊 by starring our repos, sharing our libraries and becoming a sponsor ⭐.

@lazulit3 lazulit3 deleted the fix-derivecolumn-fromstr-include-column_name branch June 11, 2025 06:04
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.

2 participants
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载