From fb94dd1153f084fad7a78bbe7adf7740a417bfb1 Mon Sep 17 00:00:00 2001 From: Automated Action Date: Mon, 12 May 2025 16:37:27 +0000 Subject: [PATCH] Fix updated_at column in migration and schema to ensure auto-update on record changes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- alembic/env.py | 6 ++++++ alembic/versions/a12b34c56789_create_todos_table.py | 2 +- app/schemas/todo.py | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/alembic/env.py b/alembic/env.py index 07a0d15..f1476b9 100644 --- a/alembic/env.py +++ b/alembic/env.py @@ -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: diff --git a/alembic/versions/a12b34c56789_create_todos_table.py b/alembic/versions/a12b34c56789_create_todos_table.py index 74dc68d..6c7075d 100644 --- a/alembic/versions/a12b34c56789_create_todos_table.py +++ b/alembic/versions/a12b34c56789_create_todos_table.py @@ -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) diff --git a/app/schemas/todo.py b/app/schemas/todo.py index 68180f2..9bef72d 100644 --- a/app/schemas/todo.py +++ b/app/schemas/todo.py @@ -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 \ No newline at end of file