我正在使用SQLAlchemy(1.2.5),pyodbc(4.0.22)。我使用python / SQLAlchemy创建了一个表,如下所示:
class Persons(Base):
__tablename__ = 'Persons'
ID_Person = Column(Integer(),primary_key = True)
Affiliate_Name = Column(VARCHAR(200), unique=True, nullable = False)
time_created = Column(DateTime(timezone=True), server_default=func.now())
time_updated = Column(DateTime(timezone=True), server_onupdate=func.now())
然后我可以进入MS SQLServer(2014)并查看创建的表。
我在SQL Server中执行以下命令:
USE [TestDB]
INSERT INTO [dbo].[Persons] (Affiliate_Name) values ('RANDOM NAME')
UPDATE [dbo].[Persons]
SET Affiliate_Name = 'RANDOM NAME2'
WHERE Affiliate_Name = 'RANDOM NAME'
当我查看数据时,time_updated列仍为NULL。
编辑:
我将表格定义更改为以下内容:
class Persons(Base):
__tablename__ = 'Persons'
ID_Person = Column(Integer(),primary_key = True)
Affiliate_Name = Column(VARCHAR(200), unique=True, nullable = False)
time_created = Column(DateTime(timezone=True), server_default=func.now())
time_updated = Column(DateTime(timezone=True), onupdate=datetime.now)
现在,当SQLAlchemy更新数据时,time_updated列会更新,但需要在MSSQL中更改触发器