From fea009a9f1d05a07feb7c012e31e836ce0cddb9a Mon Sep 17 00:00:00 2001 From: witcher Date: Wed, 6 Jul 2022 17:38:17 +0200 Subject: [PATCH 1/5] Add CLI option to skip primary keys with serde Implements: https://github.com/SeaQL/sea-orm/issues/841 --- sea-orm-cli/src/cli.rs | 7 ++++ sea-orm-cli/src/commands.rs | 12 +++--- sea-orm-codegen/src/entity/writer.rs | 63 +++++++++++++++++++++++----- 3 files changed, 67 insertions(+), 15 deletions(-) diff --git a/sea-orm-cli/src/cli.rs b/sea-orm-cli/src/cli.rs index df86fe68f..d169515c0 100644 --- a/sea-orm-cli/src/cli.rs +++ b/sea-orm-cli/src/cli.rs @@ -166,5 +166,12 @@ pub enum GenerateSubcommands { help = "Automatically derive serde Serialize / Deserialize traits for the entity (none, serialize, deserialize, both)" )] with_serde: String, + + #[clap( + action, + long, + help = "Generate a serde field attribute for the primary keys to skip them during deserialization if they're not present" + )] + skip_primary_key_deserialization: bool, }, } diff --git a/sea-orm-cli/src/commands.rs b/sea-orm-cli/src/commands.rs index 7d37dc82b..9278ef004 100644 --- a/sea-orm-cli/src/commands.rs +++ b/sea-orm-cli/src/commands.rs @@ -23,6 +23,7 @@ pub async fn run_generate_command( database_schema, database_url, with_serde, + skip_primary_key_deserialization, } => { if verbose { let _ = tracing_subscriber::fmt() @@ -88,9 +89,7 @@ pub async fn run_generate_command( } }; - let filter_skip_tables = |table: &String| -> bool { - !ignore_tables.contains(table) - }; + let filter_skip_tables = |table: &String| -> bool { !ignore_tables.contains(table) }; let database_name = if !is_sqlite { // The database name should be the first element of the path string @@ -175,8 +174,11 @@ pub async fn run_generate_command( _ => unimplemented!("{} is not supported", url.scheme()), }; - let output = EntityTransformer::transform(table_stmts)? - .generate(expanded_format, WithSerde::from_str(&with_serde).unwrap()); + let output = EntityTransformer::transform(table_stmts)?.generate( + expanded_format, + WithSerde::from_str(&with_serde).unwrap(), + skip_primary_key_deserialization, + ); let dir = Path::new(&output_dir); fs::create_dir_all(dir)?; diff --git a/sea-orm-codegen/src/entity/writer.rs b/sea-orm-codegen/src/entity/writer.rs index 922f4bced..3bc634fad 100644 --- a/sea-orm-codegen/src/entity/writer.rs +++ b/sea-orm-codegen/src/entity/writer.rs @@ -80,9 +80,18 @@ impl FromStr for WithSerde { } impl EntityWriter { - pub fn generate(self, expanded_format: bool, with_serde: WithSerde) -> WriterOutput { + pub fn generate( + self, + expanded_format: bool, + with_serde: WithSerde, + skip_primary_key_deserialization: bool, + ) -> WriterOutput { let mut files = Vec::new(); - files.extend(self.write_entities(expanded_format, &with_serde)); + files.extend(self.write_entities( + expanded_format, + &with_serde, + skip_primary_key_deserialization, + )); files.push(self.write_mod()); files.push(self.write_prelude()); if !self.enums.is_empty() { @@ -91,7 +100,12 @@ impl EntityWriter { WriterOutput { files } } - pub fn write_entities(&self, expanded_format: bool, with_serde: &WithSerde) -> Vec { + pub fn write_entities( + &self, + expanded_format: bool, + with_serde: &WithSerde, + skip_primary_key_deserialization: bool, + ) -> Vec { self.entities .iter() .map(|entity| { @@ -112,7 +126,11 @@ impl EntityWriter { let code_blocks = if expanded_format { Self::gen_expanded_code_blocks(entity, with_serde) } else { - Self::gen_compact_code_blocks(entity, with_serde) + Self::gen_compact_code_blocks( + entity, + with_serde, + skip_primary_key_deserialization, + ) }; Self::write(&mut lines, code_blocks); OutputFile { @@ -217,10 +235,17 @@ impl EntityWriter { code_blocks } - pub fn gen_compact_code_blocks(entity: &Entity, with_serde: &WithSerde) -> Vec { + pub fn gen_compact_code_blocks( + entity: &Entity, + with_serde: &WithSerde, + skip_primary_key_deserialization: bool, + ) -> Vec { let mut imports = Self::gen_import(with_serde); imports.extend(Self::gen_import_active_enum(entity)); - let mut code_blocks = vec![imports, Self::gen_compact_model_struct(entity, with_serde)]; + let mut code_blocks = vec![ + imports, + Self::gen_compact_model_struct(entity, with_serde, skip_primary_key_deserialization), + ]; let relation_defs = if entity.get_relation_enum_name().is_empty() { vec![ Self::gen_relation_enum(entity), @@ -479,7 +504,11 @@ impl EntityWriter { } } - pub fn gen_compact_model_struct(entity: &Entity, with_serde: &WithSerde) -> TokenStream { + pub fn gen_compact_model_struct( + entity: &Entity, + with_serde: &WithSerde, + skip_primary_key_deserialization: bool, + ) -> TokenStream { let table_name = entity.table_name.as_str(); let column_names_snake_case = entity.get_column_names_snake_case(); let column_rs_types = entity.get_column_rs_types(); @@ -493,6 +522,7 @@ impl EntityWriter { .iter() .map(|col| { let mut attrs: Punctuated<_, Comma> = Punctuated::new(); + let mut skip_pk_deserialization = false; if !col.is_snake_case_name() { let column_name = &col.name; attrs.push(quote! { column_name = #column_name }); @@ -502,6 +532,12 @@ impl EntityWriter { if !col.auto_increment { attrs.push(quote! { auto_increment = false }); } + + // if deserialization with serde is even desired, set it to what the user + // wants + if with_serde == &WithSerde::Deserialize || with_serde == &WithSerde::Both { + skip_pk_deserialization = skip_primary_key_deserialization; + } } if let Some(ts) = col.get_col_type_attrs() { attrs.extend(vec![ts]); @@ -520,9 +556,16 @@ impl EntityWriter { } ts = quote! { #ts #attr }; } - quote! { - #[sea_orm(#ts)] - } + return if skip_pk_deserialization { + quote! { + #[sea_orm(#ts)] + #[serde(skip_deserialization)] + } + } else { + quote! { + #[sea_orm(#ts)] + } + }; } else { TokenStream::new() } From 7d649776127b3acfd8dd1246e24b4f9e9c2bab35 Mon Sep 17 00:00:00 2001 From: witcher Date: Tue, 2 Aug 2022 15:22:51 +0200 Subject: [PATCH 2/5] Codegen: fix tests --- sea-orm-codegen/src/entity/writer.rs | 69 ++++++++++++++++++---------- 1 file changed, 44 insertions(+), 25 deletions(-) diff --git a/sea-orm-codegen/src/entity/writer.rs b/sea-orm-codegen/src/entity/writer.rs index 3bc634fad..bd841a606 100644 --- a/sea-orm-codegen/src/entity/writer.rs +++ b/sea-orm-codegen/src/entity/writer.rs @@ -124,7 +124,7 @@ impl EntityWriter { let mut lines = Vec::new(); Self::write_doc_comment(&mut lines); let code_blocks = if expanded_format { - Self::gen_expanded_code_blocks(entity, with_serde) + Self::gen_expanded_code_blocks(entity, with_serde, false) } else { Self::gen_compact_code_blocks( entity, @@ -214,7 +214,11 @@ impl EntityWriter { lines.push("".to_owned()); } - pub fn gen_expanded_code_blocks(entity: &Entity, with_serde: &WithSerde) -> Vec { + pub fn gen_expanded_code_blocks( + entity: &Entity, + with_serde: &WithSerde, + _primary_key_auto_increment: bool, + ) -> Vec { let mut imports = Self::gen_import(with_serde); imports.extend(Self::gen_import_active_enum(entity)); let mut code_blocks = vec![ @@ -556,7 +560,7 @@ impl EntityWriter { } ts = quote! { #ts #attr }; } - return if skip_pk_deserialization { + if skip_pk_deserialization { quote! { #[sea_orm(#ts)] #[serde(skip_deserialization)] @@ -565,7 +569,7 @@ impl EntityWriter { quote! { #[sea_orm(#ts)] } - }; + } } else { TokenStream::new() } @@ -1001,13 +1005,14 @@ mod tests { } let content = lines.join(""); let expected: TokenStream = content.parse().unwrap(); - let generated = EntityWriter::gen_expanded_code_blocks(entity, &crate::WithSerde::None) - .into_iter() - .skip(1) - .fold(TokenStream::new(), |mut acc, tok| { - acc.extend(tok); - acc - }); + let generated = + EntityWriter::gen_expanded_code_blocks(entity, &crate::WithSerde::None, false) + .into_iter() + .skip(1) + .fold(TokenStream::new(), |mut acc, tok| { + acc.extend(tok); + acc + }); assert_eq!(expected.to_string(), generated.to_string()); } @@ -1041,13 +1046,14 @@ mod tests { } let content = lines.join(""); let expected: TokenStream = content.parse().unwrap(); - let generated = EntityWriter::gen_compact_code_blocks(entity, &crate::WithSerde::None) - .into_iter() - .skip(1) - .fold(TokenStream::new(), |mut acc, tok| { - acc.extend(tok); - acc - }); + let generated = + EntityWriter::gen_compact_code_blocks(entity, &crate::WithSerde::None, false) + .into_iter() + .skip(1) + .fold(TokenStream::new(), |mut acc, tok| { + acc.extend(tok); + acc + }); assert_eq!(expected.to_string(), generated.to_string()); } @@ -1067,6 +1073,7 @@ mod tests { include_str!("../../tests/compact_with_serde/cake_none.rs").into(), WithSerde::None, ), + false, Box::new(EntityWriter::gen_compact_code_blocks), )?; assert_serde_variant_results( @@ -1075,6 +1082,7 @@ mod tests { include_str!("../../tests/compact_with_serde/cake_serialize.rs").into(), WithSerde::Serialize, ), + false, Box::new(EntityWriter::gen_compact_code_blocks), )?; assert_serde_variant_results( @@ -1083,6 +1091,7 @@ mod tests { include_str!("../../tests/compact_with_serde/cake_deserialize.rs").into(), WithSerde::Deserialize, ), + false, Box::new(EntityWriter::gen_compact_code_blocks), )?; assert_serde_variant_results( @@ -1091,6 +1100,7 @@ mod tests { include_str!("../../tests/compact_with_serde/cake_both.rs").into(), WithSerde::Both, ), + false, Box::new(EntityWriter::gen_compact_code_blocks), )?; @@ -1101,6 +1111,7 @@ mod tests { include_str!("../../tests/expanded_with_serde/cake_none.rs").into(), WithSerde::None, ), + false, Box::new(EntityWriter::gen_expanded_code_blocks), )?; assert_serde_variant_results( @@ -1109,6 +1120,7 @@ mod tests { include_str!("../../tests/expanded_with_serde/cake_serialize.rs").into(), WithSerde::Serialize, ), + false, Box::new(EntityWriter::gen_expanded_code_blocks), )?; assert_serde_variant_results( @@ -1117,6 +1129,7 @@ mod tests { include_str!("../../tests/expanded_with_serde/cake_deserialize.rs").into(), WithSerde::Deserialize, ), + false, Box::new(EntityWriter::gen_expanded_code_blocks), )?; assert_serde_variant_results( @@ -1125,6 +1138,7 @@ mod tests { include_str!("../../tests/expanded_with_serde/cake_both.rs").into(), WithSerde::Both, ), + false, Box::new(EntityWriter::gen_expanded_code_blocks), )?; @@ -1134,7 +1148,8 @@ mod tests { fn assert_serde_variant_results( cake_entity: &Entity, entity_serde_variant: &(String, WithSerde), - generator: Box Vec>, + primary_key_auto_increment: bool, + generator: Box Vec>, ) -> io::Result<()> { let mut reader = BufReader::new(entity_serde_variant.0.as_bytes()); let mut lines: Vec = Vec::new(); @@ -1148,12 +1163,16 @@ mod tests { } let content = lines.join(""); let expected: TokenStream = content.parse().unwrap(); - let generated = generator(cake_entity, &entity_serde_variant.1) - .into_iter() - .fold(TokenStream::new(), |mut acc, tok| { - acc.extend(tok); - acc - }); + let generated = generator( + cake_entity, + &entity_serde_variant.1, + primary_key_auto_increment, + ) + .into_iter() + .fold(TokenStream::new(), |mut acc, tok| { + acc.extend(tok); + acc + }); assert_eq!(expected.to_string(), generated.to_string()); Ok(()) From 0ee26b4b17349a733cc7b679eadea13e3004d1e1 Mon Sep 17 00:00:00 2001 From: tsar-boomba <77396670+tsar-boomba@users.noreply.github.com> Date: Thu, 3 Nov 2022 12:32:16 -0400 Subject: [PATCH 3/5] complete skip_deserialize cli feature --- sea-orm-cli/src/cli.rs | 2 +- sea-orm-codegen/src/entity/writer.rs | 49 ++++++++++++++++--- .../tests/compact_with_serde/cake_both.rs | 1 + .../compact_with_serde/cake_deserialize.rs | 1 + .../tests/expanded_with_serde/cake_both.rs | 1 + .../expanded_with_serde/cake_deserialize.rs | 1 + 6 files changed, 46 insertions(+), 9 deletions(-) diff --git a/sea-orm-cli/src/cli.rs b/sea-orm-cli/src/cli.rs index 191b62513..01531e795 100644 --- a/sea-orm-cli/src/cli.rs +++ b/sea-orm-cli/src/cli.rs @@ -179,7 +179,7 @@ pub enum GenerateSubcommands { #[clap( action, long, - help = "Generate a serde field attribute for the primary keys to skip them during deserialization if they're not present" + help = "Generate a serde field attribute for the primary keys to skip them during deserialization if they're not present, must also use '--with-serde' 'both' or 'deserialize'" )] skip_primary_key_deserialization: bool, }, diff --git a/sea-orm-codegen/src/entity/writer.rs b/sea-orm-codegen/src/entity/writer.rs index f45739d64..46c2c40dc 100644 --- a/sea-orm-codegen/src/entity/writer.rs +++ b/sea-orm-codegen/src/entity/writer.rs @@ -134,6 +134,8 @@ impl EntityWriter { .iter() .map(|column| column.get_info(&context.date_time_crate)) .collect::>(); + // use must have serde enabled to use this + let skip_primary_key_deserialization = context.skip_primary_key_deserialization && (context.with_serde == WithSerde::Both || context.with_serde == WithSerde::Deserialize); info!("Generating {}", entity_file); for info in column_info.iter() { @@ -148,7 +150,7 @@ impl EntityWriter { &context.with_serde, &context.date_time_crate, &context.schema_name, - context.skip_primary_key_deserialization, + skip_primary_key_deserialization, ) } else { Self::gen_compact_code_blocks( @@ -156,7 +158,7 @@ impl EntityWriter { &context.with_serde, &context.date_time_crate, &context.schema_name, - context.skip_primary_key_deserialization, + skip_primary_key_deserialization, ) }; Self::write(&mut lines, code_blocks); @@ -246,7 +248,7 @@ impl EntityWriter { with_serde: &WithSerde, date_time_crate: &DateTimeCrate, schema_name: &Option, - _skip_primary_key_deserialization: bool, + skip_primary_key_deserialization: bool, ) -> Vec { let mut imports = Self::gen_import(with_serde); imports.extend(Self::gen_import_active_enum(entity)); @@ -254,7 +256,12 @@ impl EntityWriter { imports, Self::gen_entity_struct(), Self::gen_impl_entity_name(entity, schema_name), - Self::gen_model_struct(entity, with_serde, date_time_crate), + Self::gen_model_struct( + entity, + with_serde, + date_time_crate, + skip_primary_key_deserialization, + ), Self::gen_column_enum(entity), Self::gen_primary_key_enum(entity), Self::gen_impl_primary_key(entity, date_time_crate), @@ -381,16 +388,41 @@ impl EntityWriter { entity: &Entity, with_serde: &WithSerde, date_time_crate: &DateTimeCrate, + skip_primary_key_deserialization: bool, ) -> TokenStream { let column_names_snake_case = entity.get_column_names_snake_case(); let column_rs_types = entity.get_column_rs_types(date_time_crate); + let primary_keys: Vec = entity + .primary_keys + .iter() + .map(|pk| pk.name.clone()) + .collect(); + let fields = column_names_snake_case + .into_iter() + .enumerate() + .fold(TokenStream::new(), |tokens, (i, field_name)| { + let field_type = column_rs_types.get(i).unwrap(); + let is_primary_key = primary_keys.contains(&field_name.to_string()); + if is_primary_key && skip_primary_key_deserialization { + quote! { + #tokens + pub #field_name: #field_type + } + } else { + quote! { + #tokens + #[serde(skip_deserialization)] + pub #field_name: #field_type + } + } + }); let extra_derive = with_serde.extra_derive(); quote! { #[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel #extra_derive)] pub struct Model { - #(pub #column_names_snake_case: #column_rs_types,)* + #fields } } } @@ -581,12 +613,13 @@ impl EntityWriter { .iter() .map(|col| { let mut attrs: Punctuated<_, Comma> = Punctuated::new(); + let is_primary_key = primary_keys.contains(&col.name); let mut skip_pk_deserialization = false; if !col.is_snake_case_name() { let column_name = &col.name; attrs.push(quote! { column_name = #column_name }); } - if primary_keys.contains(&col.name) { + if is_primary_key { attrs.push(quote! { primary_key }); if !col.auto_increment { attrs.push(quote! { auto_increment = false }); @@ -615,7 +648,7 @@ impl EntityWriter { } ts = quote! { #ts #attr }; } - if skip_pk_deserialization { + if is_primary_key && skip_pk_deserialization { quote! { #[sea_orm(#ts)] #[serde(skip_deserialization)] @@ -1349,7 +1382,7 @@ mod tests { &entity_serde_variant.1, &DateTimeCrate::Chrono, &entity_serde_variant.2, - false, + true, ) .into_iter() .fold(TokenStream::new(), |mut acc, tok| { diff --git a/sea-orm-codegen/tests/compact_with_serde/cake_both.rs b/sea-orm-codegen/tests/compact_with_serde/cake_both.rs index 3a1bea9ab..ddc8b1767 100644 --- a/sea-orm-codegen/tests/compact_with_serde/cake_both.rs +++ b/sea-orm-codegen/tests/compact_with_serde/cake_both.rs @@ -7,6 +7,7 @@ use serde::{Deserialize, Serialize}; #[sea_orm(table_name = "cake")] pub struct Model { #[sea_orm(primary_key)] + #[serde(skip_deserialization)] pub id: i32, #[sea_orm(column_type = "Text", nullable)] pub name: Option , diff --git a/sea-orm-codegen/tests/compact_with_serde/cake_deserialize.rs b/sea-orm-codegen/tests/compact_with_serde/cake_deserialize.rs index b36718f94..d1ef4d454 100644 --- a/sea-orm-codegen/tests/compact_with_serde/cake_deserialize.rs +++ b/sea-orm-codegen/tests/compact_with_serde/cake_deserialize.rs @@ -7,6 +7,7 @@ use serde::Deserialize; #[sea_orm(table_name = "cake")] pub struct Model { #[sea_orm(primary_key)] + #[serde(skip_deserialization)] pub id: i32, #[sea_orm(column_type = "Text", nullable)] pub name: Option , diff --git a/sea-orm-codegen/tests/expanded_with_serde/cake_both.rs b/sea-orm-codegen/tests/expanded_with_serde/cake_both.rs index 888211350..717c79321 100644 --- a/sea-orm-codegen/tests/expanded_with_serde/cake_both.rs +++ b/sea-orm-codegen/tests/expanded_with_serde/cake_both.rs @@ -14,6 +14,7 @@ impl EntityName for Entity { #[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel, Serialize, Deserialize)] pub struct Model { + #[serde(skip_deserialization)] pub id: i32, pub name: Option , } diff --git a/sea-orm-codegen/tests/expanded_with_serde/cake_deserialize.rs b/sea-orm-codegen/tests/expanded_with_serde/cake_deserialize.rs index 068f17dab..ca1251a14 100644 --- a/sea-orm-codegen/tests/expanded_with_serde/cake_deserialize.rs +++ b/sea-orm-codegen/tests/expanded_with_serde/cake_deserialize.rs @@ -14,6 +14,7 @@ impl EntityName for Entity { #[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel, Deserialize)] pub struct Model { + #[serde(skip_deserialization)] pub id: i32, pub name: Option , } From 07e293e3e7d96fe61d2d081a65188d736410f9fc Mon Sep 17 00:00:00 2001 From: tsar-boomba <77396670+tsar-boomba@users.noreply.github.com> Date: Thu, 3 Nov 2022 12:43:46 -0400 Subject: [PATCH 4/5] run fmt --- sea-orm-codegen/src/entity/writer.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/sea-orm-codegen/src/entity/writer.rs b/sea-orm-codegen/src/entity/writer.rs index aa1f9a650..13d029340 100644 --- a/sea-orm-codegen/src/entity/writer.rs +++ b/sea-orm-codegen/src/entity/writer.rs @@ -143,7 +143,9 @@ impl EntityWriter { .map(|column| column.get_info(&context.date_time_crate)) .collect::>(); // use must have serde enabled to use this - let skip_primary_key_deserialization = context.skip_primary_key_deserialization && (context.with_serde == WithSerde::Both || context.with_serde == WithSerde::Deserialize); + let skip_primary_key_deserialization = context.skip_primary_key_deserialization + && (context.with_serde == WithSerde::Both + || context.with_serde == WithSerde::Deserialize); info!("Generating {}", entity_file); for info in column_info.iter() { @@ -408,10 +410,9 @@ impl EntityWriter { .iter() .map(|pk| pk.name.clone()) .collect(); - let fields = column_names_snake_case - .into_iter() - .enumerate() - .fold(TokenStream::new(), |tokens, (i, field_name)| { + let fields = column_names_snake_case.into_iter().enumerate().fold( + TokenStream::new(), + |tokens, (i, field_name)| { let field_type = column_rs_types.get(i).unwrap(); let is_primary_key = primary_keys.contains(&field_name.to_string()); if is_primary_key && skip_primary_key_deserialization { @@ -426,7 +427,8 @@ impl EntityWriter { pub #field_name: #field_type } } - }); + }, + ); let extra_derive = with_serde.extra_derive(); From f233ab538ae0fd4d5de29e709e7ded57479add09 Mon Sep 17 00:00:00 2001 From: tsar-boomba <77396670+tsar-boomba@users.noreply.github.com> Date: Fri, 4 Nov 2022 09:25:16 -0400 Subject: [PATCH 5/5] fix tests --- sea-orm-codegen/src/entity/writer.rs | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/sea-orm-codegen/src/entity/writer.rs b/sea-orm-codegen/src/entity/writer.rs index 0a570e5e2..97e268c1a 100644 --- a/sea-orm-codegen/src/entity/writer.rs +++ b/sea-orm-codegen/src/entity/writer.rs @@ -418,13 +418,13 @@ impl EntityWriter { if is_primary_key && skip_primary_key_deserialization { quote! { #tokens - pub #field_name: #field_type + #[serde(skip_deserialization)] + pub #field_name: #field_type, } } else { quote! { #tokens - #[serde(skip_deserialization)] - pub #field_name: #field_type + pub #field_name: #field_type, } } }, @@ -631,7 +631,6 @@ impl EntityWriter { .map(|col| { let mut attrs: Punctuated<_, Comma> = Punctuated::new(); let is_primary_key = primary_keys.contains(&col.name); - let mut skip_pk_deserialization = false; if !col.is_snake_case_name() { let column_name = &col.name; attrs.push(quote! { column_name = #column_name }); @@ -641,12 +640,6 @@ impl EntityWriter { if !col.auto_increment { attrs.push(quote! { auto_increment = false }); } - - // if deserialization with serde is even desired, set it to what the user - // wants - if with_serde == &WithSerde::Deserialize || with_serde == &WithSerde::Both { - skip_pk_deserialization = skip_primary_key_deserialization; - } } if let Some(ts) = col.get_col_type_attrs() { attrs.extend(vec![ts]); @@ -665,7 +658,7 @@ impl EntityWriter { } ts = quote! { #ts #attr }; } - if is_primary_key && skip_pk_deserialization { + if is_primary_key && skip_primary_key_deserialization { quote! { #[sea_orm(#ts)] #[serde(skip_deserialization)] @@ -1552,6 +1545,8 @@ mod tests { ) -> io::Result<()> { let mut reader = BufReader::new(entity_serde_variant.0.as_bytes()); let mut lines: Vec = Vec::new(); + let skip_primary_key_deserialization = entity_serde_variant.1 == WithSerde::Both + || entity_serde_variant.1 == WithSerde::Deserialize; reader.read_until(b'\n', &mut Vec::new())?; @@ -1562,12 +1557,13 @@ mod tests { } let content = lines.join(""); let expected: TokenStream = content.parse().unwrap(); + println!("{:?}", entity_serde_variant.1); let generated = generator( cake_entity, &entity_serde_variant.1, &DateTimeCrate::Chrono, &entity_serde_variant.2, - true, + skip_primary_key_deserialization, ) .into_iter() .fold(TokenStream::new(), |mut acc, tok| {