From 01011cf0f4913be2b903027e68a17fe289b8bfc2 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Wed, 15 Sep 2021 10:46:12 +0800 Subject: [PATCH] Generate macro attribute "nullable" --- sea-orm-codegen/rustfmt.toml | 3 ++- sea-orm-codegen/src/entity/column.rs | 1 + sea-orm-codegen/src/entity/writer.rs | 7 +++++-- sea-orm-codegen/tests/compact/cake.rs | 3 ++- sea-orm-codegen/tests/expanded/cake.rs | 4 ++-- 5 files changed, 12 insertions(+), 6 deletions(-) diff --git a/sea-orm-codegen/rustfmt.toml b/sea-orm-codegen/rustfmt.toml index 2555d91f..d7f6da65 100644 --- a/sea-orm-codegen/rustfmt.toml +++ b/sea-orm-codegen/rustfmt.toml @@ -1,3 +1,4 @@ ignore = [ - "tests/entity/*.rs", + "tests/compact/*.rs", + "tests/expanded/*.rs", ] \ No newline at end of file diff --git a/sea-orm-codegen/src/entity/column.rs b/sea-orm-codegen/src/entity/column.rs index 7a6b1759..dfec4b8e 100644 --- a/sea-orm-codegen/src/entity/column.rs +++ b/sea-orm-codegen/src/entity/column.rs @@ -58,6 +58,7 @@ impl Column { ColumnType::Double(Some(l)) => Some(format!("Double(Some({}))", l)), ColumnType::Decimal(Some((p, s))) => Some(format!("Decimal(Some(({}, {})))", p, s)), ColumnType::Money(Some((p, s))) => Some(format!("Money(Some({}, {}))", p, s)), + ColumnType::Text => Some("Text".to_owned()), ColumnType::Custom(iden) => { Some(format!("Custom(\"{}\".to_owned())", iden.to_string())) } diff --git a/sea-orm-codegen/src/entity/writer.rs b/sea-orm-codegen/src/entity/writer.rs index 448c5197..edfbef0b 100644 --- a/sea-orm-codegen/src/entity/writer.rs +++ b/sea-orm-codegen/src/entity/writer.rs @@ -342,6 +342,9 @@ impl EntityWriter { } if let Some(ts) = col.get_col_type_attrs() { attrs.extend(vec![ts]); + if !col.not_null { + attrs.push(quote! { nullable }); + } }; if !attrs.is_empty() { let mut ts = TokenStream::new(); @@ -410,9 +413,9 @@ mod tests { }, Column { name: "name".to_owned(), - col_type: ColumnType::String(Some(255)), + col_type: ColumnType::Text, auto_increment: false, - not_null: true, + not_null: false, unique: false, }, ], diff --git a/sea-orm-codegen/tests/compact/cake.rs b/sea-orm-codegen/tests/compact/cake.rs index 4a64611e..2e26257c 100644 --- a/sea-orm-codegen/tests/compact/cake.rs +++ b/sea-orm-codegen/tests/compact/cake.rs @@ -7,7 +7,8 @@ use sea_orm::entity::prelude::*; pub struct Model { #[sea_orm(primary_key)] pub id: i32, - pub name: String, + #[sea_orm(column_type = "Text", nullable)] + pub name: Option , } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] diff --git a/sea-orm-codegen/tests/expanded/cake.rs b/sea-orm-codegen/tests/expanded/cake.rs index 55fa279f..0b33b618 100644 --- a/sea-orm-codegen/tests/expanded/cake.rs +++ b/sea-orm-codegen/tests/expanded/cake.rs @@ -14,7 +14,7 @@ impl EntityName for Entity { #[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)] pub struct Model { pub id: i32, - pub name: String, + pub name: Option , } #[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)] @@ -46,7 +46,7 @@ impl ColumnTrait for Column { fn def(&self) -> ColumnDef { match self { Self::Id => ColumnType::Integer.def(), - Self::Name => ColumnType::String(Some(255u32)).def(), + Self::Name => ColumnType::Text.def().null(), } } }