-
-
Notifications
You must be signed in to change notification settings - Fork 593
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
fix: include column_name in DeriveColumn FromStr impl #2603
Conversation
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`.
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) | ||
)) |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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
🎉 Released In 1.1.12 🎉Thank you everyone for the contribution! |
PR Info
I'm pretty sure this fixes the cause of:
load_many
call with different column name on primary key #1870I encountered the same error when trying to use
load_many()
My generated entities have column names in an (almost) UpperCamelCase style:
#[sea_orm(column_name = "UpperCamelCase")]
(with some exceptions likeArtistID
)When expanding my
DeriveEntityModel
and thenDeriveColumn
, I found that theFromStr
implementation did not include my column names:This ensures that we include values from the
column_name
attributes too, like so:Bug Fixes
load_many
call with different column name on primary key #1870Changes
Column::from_str()
now recognizes values from#[sea_orm(column_name = "...")]
attributes set on:Column
variants (whenColumn
is an expanded definition withDeriveColumn
)Model
fields (whenDeriveEntityModel
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 acolumn_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.