Generate macro attribute "nullable"

This commit is contained in:
Billy Chan 2021-09-15 10:46:12 +08:00
parent ca3dd2d2a1
commit 01011cf0f4
No known key found for this signature in database
GPG Key ID: A2D690CAC7DF3CC7
5 changed files with 12 additions and 6 deletions

View File

@ -1,3 +1,4 @@
ignore = [ ignore = [
"tests/entity/*.rs", "tests/compact/*.rs",
"tests/expanded/*.rs",
] ]

View File

@ -58,6 +58,7 @@ impl Column {
ColumnType::Double(Some(l)) => Some(format!("Double(Some({}))", l)), ColumnType::Double(Some(l)) => Some(format!("Double(Some({}))", l)),
ColumnType::Decimal(Some((p, s))) => Some(format!("Decimal(Some(({}, {})))", p, s)), ColumnType::Decimal(Some((p, s))) => Some(format!("Decimal(Some(({}, {})))", p, s)),
ColumnType::Money(Some((p, s))) => Some(format!("Money(Some({}, {}))", p, s)), ColumnType::Money(Some((p, s))) => Some(format!("Money(Some({}, {}))", p, s)),
ColumnType::Text => Some("Text".to_owned()),
ColumnType::Custom(iden) => { ColumnType::Custom(iden) => {
Some(format!("Custom(\"{}\".to_owned())", iden.to_string())) Some(format!("Custom(\"{}\".to_owned())", iden.to_string()))
} }

View File

@ -342,6 +342,9 @@ impl EntityWriter {
} }
if let Some(ts) = col.get_col_type_attrs() { if let Some(ts) = col.get_col_type_attrs() {
attrs.extend(vec![ts]); attrs.extend(vec![ts]);
if !col.not_null {
attrs.push(quote! { nullable });
}
}; };
if !attrs.is_empty() { if !attrs.is_empty() {
let mut ts = TokenStream::new(); let mut ts = TokenStream::new();
@ -410,9 +413,9 @@ mod tests {
}, },
Column { Column {
name: "name".to_owned(), name: "name".to_owned(),
col_type: ColumnType::String(Some(255)), col_type: ColumnType::Text,
auto_increment: false, auto_increment: false,
not_null: true, not_null: false,
unique: false, unique: false,
}, },
], ],

View File

@ -7,7 +7,8 @@ use sea_orm::entity::prelude::*;
pub struct Model { pub struct Model {
#[sea_orm(primary_key)] #[sea_orm(primary_key)]
pub id: i32, pub id: i32,
pub name: String, #[sea_orm(column_type = "Text", nullable)]
pub name: Option<String> ,
} }
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]

View File

@ -14,7 +14,7 @@ impl EntityName for Entity {
#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)] #[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
pub struct Model { pub struct Model {
pub id: i32, pub id: i32,
pub name: String, pub name: Option<String> ,
} }
#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)] #[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
@ -46,7 +46,7 @@ impl ColumnTrait for Column {
fn def(&self) -> ColumnDef { fn def(&self) -> ColumnDef {
match self { match self {
Self::Id => ColumnType::Integer.def(), Self::Id => ColumnType::Integer.def(),
Self::Name => ColumnType::String(Some(255u32)).def(), Self::Name => ColumnType::Text.def().null(),
} }
} }
} }