master
.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:
You can see that the created_at
column gets updated (from 9:55 to 7:55)
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
I expect the created_at
column to not be modified.
The created_at
column gets edited incorrectly (from 9:55 to 7:55)
No response
sqladmin version: 0.18.0
No response
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too