diff --git a/sea-orm-macros/src/derives/partial_model.rs b/sea-orm-macros/src/derives/partial_model.rs index 53f7ac173..6081ae73d 100644 --- a/sea-orm-macros/src/derives/partial_model.rs +++ b/sea-orm-macros/src/derives/partial_model.rs @@ -11,7 +11,7 @@ use syn::Expr; use syn::Meta; -use self::util::GetAsKVMeta; +use self::util::{GetAsKVMeta, GetMeta}; #[derive(Debug)] enum Error { @@ -96,39 +96,40 @@ impl DerivePartialModel { .get_as_kv("from_expr") .map(|s| syn::parse_str::(&s).map_err(Error::Syn)) .transpose()?; - skip = meta.get_as_kv("skip").is_some(); + skip = meta.exists("skip"); } } } + if skip { + continue; + } + let field_name = field.ident.unwrap(); - let col_as = match (from_col, from_expr, skip) { - (Some(col), None, false) => { + let col_as = match (from_col, from_expr) { + (None, None) => { if entity.is_none() { return Err(Error::EntityNotSpecified); } - - let field = field_name.to_string(); - ColumnAs::ColAlias { col, field } + ColumnAs::Col(format_ident!( + "{}", + field_name.to_string().to_upper_camel_case() + )) } - (None, Some(expr), false) => ColumnAs::Expr { + (None, Some(expr)) => ColumnAs::Expr { expr, field_name: field_name.to_string(), }, - (None, None, true) => { - continue; - } - (None, None, false) => { + (Some(col), None) => { if entity.is_none() { return Err(Error::EntityNotSpecified); } - ColumnAs::Col(format_ident!( - "{}", - field_name.to_string().to_upper_camel_case() - )) + + let field = field_name.to_string(); + ColumnAs::ColAlias { col, field } } - _ => return Err(Error::MultipleSourcesSpecified(field_span)), + (Some(_), Some(_)) => return Err(Error::MultipleSourcesSpecified(field_span)), }; column_as_list.push(col_as); } @@ -229,6 +230,19 @@ mod util { } } } + + pub(super) trait GetMeta { + fn exists(&self, k: &str) -> bool; + } + + impl GetMeta for Meta { + fn exists(&self, k: &str) -> bool { + let Meta::Path(path) = self else { + return false; + }; + path.is_ident(k) + } + } } #[cfg(test)] diff --git a/tests/partial_model_tests.rs b/tests/partial_model_tests.rs index 999afa314..abbdb110e 100644 --- a/tests/partial_model_tests.rs +++ b/tests/partial_model_tests.rs @@ -56,9 +56,10 @@ struct FieldFromExpr { } #[derive(FromQueryResult, DerivePartialModel)] +#[sea_orm(entity = "Entity")] struct SkipField { #[sea_orm(from_col = "foo2")] _foo: i32, #[sea_orm(skip)] - _test_does_not_exist: Option<()>, + _bar: Option<()>, }