Skip to content

Commit

Permalink
Update updated_at with the current timestamp upon saving (#962)
Browse files Browse the repository at this point in the history
* Include updated_at upon updating of a record.

---------

Co-authored-by: Elad Kaplan <[email protected]>
Co-authored-by: Dotan J. Nahum <[email protected]>
  • Loading branch information
3 people authored Nov 9, 2024
1 parent e1a334a commit 2b34485
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,22 @@ fn fix_entities() -> AppResult<()> {
use super::_entities::{module}::{{ActiveModel, Entity}};
pub type {module_pascal} = Entity;
#[async_trait::async_trait]
impl ActiveModelBehavior for ActiveModel {{
// extend activemodel below (keep comment for generators)
async fn before_save<C>(self, _db: &C, insert: bool) -> std::result::Result<Self, DbErr>
where
C: ConnectionTrait,
{{
if !insert && self.updated_at.is_unchanged() {{
let mut this = self;
this.updated_at = sea_orm::ActiveValue::Set(chrono::Utc::now().into());
Ok(this)
}} else {{
Ok(self)
}}
}}
}}
"
),
Expand Down

0 comments on commit 2b34485

Please sign in to comment.