I’m trying to automatically fill in created_at and updated_at for my database records.
Here is my base class:
class ModelBase(DeclarativeBase):
created_by = mapped_column(String)
created_at = mapped_column(DateTime, default=func.now())
updated_by = mapped_column(String)
updated_at = mapped_column(DateTime, onupdate=func.now())
created_at
gets filled in correctly when I first create the record. But updated_at
does not
Here is some sample output:
Field CreatedAt UpdatedAt
-----------------------------------------
module3 None 2024-06-25 10:40:27.072737 None
module3 True 2024-06-25 10:40:27.072737 None
The field
is simply a field that I updated. When the record is first created, it fills in CreatedAt and leaves UpdatedAt blank. When I update, UpdatedAt is not getting changed.