sqladmin icon indicating copy to clipboard operation
sqladmin copied to clipboard

Datetime with timezone edited incorrectly

Open Azkae opened this issue 7 months ago • 2 comments

Checklist

  • [X] The bug is reproducible against the latest release or master.
  • [X] There are no similar issues or pull requests to fix it yet.

Describe the bug

Datetime with timezone are edited incorrectly. In this example I have a table with a name and a created_at column:

class User(Base):
    __tablename__ = "users"

    id: Mapped[int] = mapped_column(primary_key=True)
    name: Mapped[str]
    created_at: Mapped[dt.datetime] = mapped_column(
        DateTime(timezone=True), server_default=func.now()
    )

If I change the name of a row, it will also edit the created_at column, most likely because the timezone is missing from the edit field:

Screenshot 2024-07-23 at 11 55 18 Screenshot 2024-07-23 at 11 55 30 Screenshot 2024-07-23 at 11 55 35

You can see that the created_at column gets updated (from 9:55 to 7:55)

Steps to reproduce the bug

Here is the complete program used in the example:

from contextlib import asynccontextmanager
import datetime as dt
from sqlalchemy import DateTime, func, text
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
from sqlalchemy.orm import Mapped, declarative_base, mapped_column

from fastapi import FastAPI
from sqladmin import Admin, ModelView


Base = declarative_base()
engine = create_async_engine(
    "postgresql+asyncpg://wave:@localhost/sqladmin-testing",
)


class User(Base):
    __tablename__ = "users"

    id: Mapped[int] = mapped_column(primary_key=True)
    name: Mapped[str]
    created_at: Mapped[dt.datetime] = mapped_column(
        DateTime(timezone=True), server_default=func.now()
    )


Session = async_sessionmaker(bind=engine)


@asynccontextmanager
async def lifespan(app: FastAPI):
    async with engine.begin() as conn:
        await conn.run_sync(Base.metadata.create_all)

    async with Session() as conn:
        await conn.execute(text("INSERT INTO users (name) values ('foo'), ('bar')"))
        await conn.commit()
    yield


app = FastAPI(lifespan=lifespan)
admin = Admin(app, engine)


class UserAdmin(ModelView, model=User):
    column_list = [User.id, User.name, User.created_at]


admin.add_view(UserAdmin)

The issue doesn't happen with SQLite, only with PostgreSQL

Expected behavior

I expect the created_at column to not be modified.

Actual behavior

The created_at column gets edited incorrectly (from 9:55 to 7:55)

Debugging material

No response

Environment

sqladmin version: 0.18.0

Additional context

No response

Azkae avatar Jul 23 '24 10:07 Azkae