SQLAlchemy 标量 () returns 字符串用于 DATETIME('now') 与 SQLite

SQLAlchemy scalar() returns string for DATETIME('now') with SQLite

我有以下代码:

if settings.DATABASE_URL.startswith("sqlite"):  
    CURRENT_TIME_QUERY = "SELECT DATETIME('now')" 
else:
    CURRENT_TIME_QUERY = "SELECT CURRENT_TIMESTAMP"

timestamp = session.execute(CURRENT_TIME_QUERY).scalar()

当我运行此代码针对MySQL时,timestamp将是一个datetime对象,但在SQLite的情况下我获取带有实际时间戳的字符串。 SQLAlchemy 以不同方式处理这些情况,还是查询的错误?我希望始终有一个 datetime 对象。

对于像您这样的简单查询,SQLAlchemy 只是返回驱动程序(DBAPI 层)提供的内容。 MySQL 驱动程序(例如 mysqldb)返回一个 datetime.datetime 对象,而 SQLite 驱动程序返回一个 <class 'str'>。如问题评论和 SQLite documentation:

中所述

SQLite does not have a storage class set aside for storing dates and/or times.

请注意,如果您有一个 ORM 对象或一个核心 Table,其列定义为 sqlalchemy.DateTime,则 sqlite 方言将为您将返回的字符串值转换为 datetime

with engine.begin() as conn:
    conn.exec_driver_sql("CREATE TABLE thing (id int primary key, dt datetime)")
    conn.exec_driver_sql("INSERT INTO thing (id, dt) VALUES (1, '2021-09-02 08:50:12')")


class Thing(Base):
    __tablename__ = "thing"
    id = Column(Integer, primary_key=True, autoincrement=False)
    dt = Column(DateTime)


with Session(engine) as session:
    result = session.execute(select(Thing.dt)).scalar()
    print(repr(result))  # datetime.datetime(2021, 9, 2, 8, 50, 12)

但是,对于普通查询,您必须检查返回值是否为 str 并自行将其转换为 datetime