diff --git a/sea-orm-macros/src/derives/entity_model.rs b/sea-orm-macros/src/derives/entity_model.rs index 3ce2801e4..d9abf8e94 100644 --- a/sea-orm-macros/src/derives/entity_model.rs +++ b/sea-orm-macros/src/derives/entity_model.rs @@ -137,7 +137,15 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec) -> syn::Res } else if meta.path.is_ident("default_value") { default_value = Some(meta.value()?.parse::()?); } else if meta.path.is_ident("default_expr") { - default_expr = Some(meta.value()?.parse::()?); + let lit = meta.value()?.parse()?; + if let Lit::Str(litstr) = lit { + let value_expr: TokenStream = syn::parse_str(&litstr.value())?; + default_expr = Some(value_expr); + } else { + return Err( + meta.error(format!("Invalid column_type {:?}", lit)) + ); + } } else if meta.path.is_ident("column_name") { let lit = meta.value()?.parse()?; if let Lit::Str(litstr) = lit { @@ -264,10 +272,10 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec) -> syn::Res match_row = quote! { #match_row.unique() }; } if let Some(default_value) = default_value { - match_row = quote! { #match_row.default_value(#default_value) }; + match_row = quote! { #match_row.default(#default_value) }; } if let Some(default_expr) = default_expr { - match_row = quote! { #match_row.default_expr(#default_expr) }; + match_row = quote! { #match_row.default(#default_expr) }; } columns_trait.push(match_row); } diff --git a/src/entity/column.rs b/src/entity/column.rs index b263413fd..7569d908d 100644 --- a/src/entity/column.rs +++ b/src/entity/column.rs @@ -15,7 +15,7 @@ pub struct ColumnDef { pub(crate) null: bool, pub(crate) unique: bool, pub(crate) indexed: bool, - pub(crate) default_value: Option, + pub(crate) default: Option, } macro_rules! bind_oper { @@ -310,7 +310,7 @@ impl ColumnTypeTrait for ColumnType { null: false, unique: false, indexed: false, - default_value: None, + default: None, } } @@ -362,11 +362,21 @@ impl ColumnDef { } /// Set the default value + #[deprecated(since = "0.12.0", note = "Please use [`ColumnDef::default`]")] pub fn default_value(mut self, value: T) -> Self where T: Into, { - self.default_value = Some(value.into()); + self.default = Some(value.into().into()); + self + } + + /// Set the default value or expression of a column + pub fn default(mut self, default: T) -> Self + where + T: Into, + { + self.default = Some(default.into()); self } @@ -493,7 +503,7 @@ mod tests { #[test] #[cfg(feature = "macros")] fn entity_model_column_1() { - use crate::entity::*; + use crate::prelude::*; mod hello { use crate as sea_orm; @@ -521,6 +531,12 @@ mod tests { pub eight: u32, #[sea_orm(unique, indexed, nullable)] pub nine: u64, + #[sea_orm(default_expr = "Expr::current_timestamp()")] + pub ten: DateTimeUtc, + #[sea_orm(default_value = 7)] + pub eleven: u8, + #[sea_orm(default_value = "twelve_value")] + pub twelve: String, } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] @@ -562,6 +578,20 @@ mod tests { hello::Column::Nine.def(), ColumnType::BigUnsigned.def().unique().indexed().nullable() ); + assert_eq!( + hello::Column::Ten.def(), + ColumnType::TimestampWithTimeZone + .def() + .default(Expr::current_timestamp()) + ); + assert_eq!( + hello::Column::Eleven.def(), + ColumnType::TinyUnsigned.def().default(7) + ); + assert_eq!( + hello::Column::Twelve.def(), + ColumnType::String(None).def().default("twelve_value") + ); } #[test] diff --git a/src/entity/prelude.rs b/src/entity/prelude.rs index ff6d779c5..fb326808a 100644 --- a/src/entity/prelude.rs +++ b/src/entity/prelude.rs @@ -1,6 +1,6 @@ pub use crate::{ error::*, - sea_query::{BlobSize, DynIden, RcOrArc, SeaRc}, + sea_query::{BlobSize, DynIden, Expr, RcOrArc, SeaRc}, ActiveEnum, ActiveModelBehavior, ActiveModelTrait, ColumnDef, ColumnTrait, ColumnType, ColumnTypeTrait, ConnectionTrait, CursorTrait, DatabaseConnection, DbConn, EntityName, EntityTrait, EnumIter, ForeignKeyAction, Iden, IdenStatic, Linked, LoaderTrait, ModelTrait, diff --git a/src/schema/entity.rs b/src/schema/entity.rs index 7aaac164d..f6e9ead01 100644 --- a/src/schema/entity.rs +++ b/src/schema/entity.rs @@ -210,8 +210,8 @@ where if orm_column_def.unique { column_def.unique_key(); } - if let Some(value) = orm_column_def.default_value { - column_def.default(value); + if let Some(default) = orm_column_def.default { + column_def.default(default); } for primary_key in E::PrimaryKey::iter() { if column.to_string() == primary_key.into_column().to_string() {