Fix updated_at column in migration and schema to ensure auto-update on record changes

Fixed two issues:
1. Added onupdate and server_default to updated_at column in migration script
2. Changed updated_at to be non-optional in TodoResponse schema

🤖 Generated with BackendIM... (backend.im)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Automated Action 2025-05-12 16:37:27 +00:00
parent 547635aebe
commit fb94dd1153
3 changed files with 8 additions and 2 deletions

View File

@ -9,6 +9,12 @@ from alembic import context
# access to the values within the .ini file in use.
config = context.config
# Import database configuration
from app.db.database import SQLALCHEMY_DATABASE_URL
# Override sqlalchemy.url in alembic.ini with the one from app config
config.set_main_option("sqlalchemy.url", SQLALCHEMY_DATABASE_URL)
# Interpret the config file for Python logging.
# This line sets up loggers basically.
if config.config_file_name is not None:

View File

@ -24,7 +24,7 @@ def upgrade() -> None:
sa.Column('description', sa.String(), nullable=True),
sa.Column('completed', sa.Boolean(), default=False),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.func.now()),
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.func.now(), onupdate=sa.func.now()),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_todos_id'), 'todos', ['id'], unique=False)

View File

@ -26,7 +26,7 @@ class TodoUpdate(BaseModel):
class TodoResponse(TodoBase):
id: int
created_at: datetime
updated_at: Optional[datetime] = None
updated_at: datetime
class Config:
from_attributes = True