我有以下SqlAlchemy模型:
class SchemaVersion(Base):
__tablename__ = 'schema_version'
timestamp = Column(DateTime, default=datetime.datetime.utcnow, primary_key=True)
version = Column(String)
notes = Column(String)
最接近我的是:
statement = insert(SchemaVersion).values(version='v1.0.0',
notes='Initial schema')
print(statement.compile(engine, compile_kwargs={'literal_binds': True}))
engine
是我使用的引擎(Postgres)
结果是:
INSERT INTO schema_version (timestamp, version, notes) VALUES (%(timestamp)s, 'v1.0.0', 'Initial schema')
问题当然是%(timestamp)s
如何将now()
作为值传递,或者让SqlAlchemy为我使用默认值?如果我尝试:
statement = insert(SchemaVersion).values(timestamp=datetime.datetime.utcnow(),
version='v1.0.0',
notes='Initial schema')
我得到一个错误:
NotImplementedError: Don't know how to literal-quote value datetime.datetime(2018, 8, 21, 9, 50, 11, 732957)
虽然不太清楚为什么您对生成带有绑定文本的SQL字符串感兴趣,但是您可以通过在数据库中使用例如} and ^{} are supported :
CURRENT_TIMESTAMP
生成时间戳来避免only simple types such as ^{这将按照
相关问题 更多 >
编程相关推荐