我的sqlalchemy代码需要同时支持sqlite和postgres,但是现在它不支持sqlite。
sqlalchemy.exc.StatementError:(buildins.TypeError)SQLite DateTime类型只接受Python日期时间和日期对象作为输入。
我检查了Error - "SQLite DateTime type only accepts Python " "datetime and date objects as input.",但在整个代码库中进行此更改是不可能的,因为它有多个地方使用了日期字符串而不是日期时间
这是我的代码,它适用于postgres引擎,但不适用于sqlite,我可以修改上面链接建议以外的任何内容,以便我的代码在sqlite和postgres上运行吗
from sqlalchemy import create_engine, Column, Integer
from sqlalchemy.orm import declarative_base, Session
from sqlalchemy.types import DateTime
Base = declarative_base()
class Foo(Base):
__tablename__ = "foo"
id = Column(Integer, primary_key=True)
col = Column(DateTime)
engine = create_engine("postgresql://tony:tony@localhost:5432")
# engine = create_engine("sqlite:///db.db")
Base.metadata.create_all(engine)
with Session(engine) as session:
session.add(Foo(col='2023-01-07T11:08:31Z'))
session.commit()
1条答案
按热度按时间pcww981p1#
DB-API规范要求SQL DATETIME以python
datetime.datetime
(docs)的形式提供。我相信
psycopg2
提供了一个扩展,可以处理ISO 8601格式的字符串,但这只是一个扩展。如果您想获得最大的兼容性,请使用
datetime.datetime
对象来传递和检索日期。另外,为什么要从
sqlalchemy.types
导入DateTime
呢?它可以直接在sqlalchemy
下获得。如果需要运行时(而不是测试)的兼容性,可以使用下面的
TypeDecorator
。如果您不想强制绑定参数,请进一步查看文档,但这将允许您在SQLite中输入ISO 8601 str。
**NB.**对于测试,如果是单元测试,则模拟数据库适配器,如果是集成(和更高)测试,则模拟use the same DB as you'll use in production。