feat: Implement automatic updated_at timestamp management in BaseModel and update BaseRepository to reflect changes
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
from datetime import UTC, datetime
|
||||
|
||||
from sqlalchemy import event
|
||||
from sqlmodel import Field, SQLModel
|
||||
|
||||
|
||||
@@ -11,3 +12,10 @@ class BaseModel(SQLModel):
|
||||
# timestamps
|
||||
created_at: datetime = Field(default_factory=lambda: datetime.now(UTC))
|
||||
updated_at: datetime = Field(default_factory=lambda: datetime.now(UTC))
|
||||
|
||||
|
||||
# SQLAlchemy event listener to automatically update updated_at timestamp
|
||||
@event.listens_for(BaseModel, "before_update", propagate=True)
|
||||
def update_timestamp(mapper, connection, target):
|
||||
"""Automatically set updated_at timestamp before update operations."""
|
||||
target.updated_at = datetime.now(UTC)
|
||||
|
||||
Reference in New Issue
Block a user