我有以下的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)
生成的打印SQL是:
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)