Fixed value for updated_at column
The onupdate
parameter on the updated_at
column can take a function that is evaluated every time the entity is updated.
This is the current code
class CreateUpdateMixin:
created_at: Mapped[int] = mapped_column(Integer, nullable=False, server_default=text("(UNIX_TIMESTAMP())"))
updated_at: Mapped[int] = mapped_column(
Integer, nullable=False, server_default=text("UNIX_TIMESTAMP()"), onupdate=round(time())
)
The time is evaluated when the server is started and then this fixed values is always set when the entity is updated. The call to time
has to be wrapped inside a lambda
function to evaluted it dynamically when it is requested.
class CreateUpdateMixin:
created_at: Mapped[int] = mapped_column(Integer, nullable=False, server_default=text("(UNIX_TIMESTAMP())"))
updated_at: Mapped[int] = mapped_column(
Integer, nullable=False, server_default=text("UNIX_TIMESTAMP()"), onupdate=lambda: round(time())
)