sqlalchemy更新无效

时间:2017-05-14 17:19:58

标签: python sqlalchemy

我使用sqlalchemy执行一些查询和更新,但是现在我遇到了UPDATE操作的问题,当我更改模型属性pet_time(ins.role_time = plus_days(ins.role_time,30))并使用会话提交时,sqlalchemy没有执行更新操作我不知道发生了什么,有人可以帮忙吗?

Base = declarative_base()

class User(Base):
    __tablename__ = 'user'
    id = Column(Integer,primary_key=True)
    nick_name = Column(String)
    create_time = Column(DateTime, default=datetime.datetime.now)
    role_time = Column(DateTime)


# the db connection info is correct
def getDbSession():
    return Session()

def queryUserById(id):
    sess = getDbSession()
    instance = sess.query(User)\
                .filter(User.id == id)\
                .limit(1).first()
    return instance

def increaseRoleTime(id,inc=1):
    ins = queryUserById(id)
    sess = getDbSession()
    if(ins.role_time is None):
        ins.role_time = datetime.datetime.now()
    inc = inc * 30
    # ins.role_time = func.ADDDATE(ins.role_time,inc)
    ins.role_time = plus_days(ins.role_time,inc)
    sess.commit()
    return 1

# import timedelta from datetime to plus days
def plus_days(mydatetime,days):
    return mydatetime + timedelta(days=days)

在函数increaseRoleTime中,我更改了属性role_time并提交,但仍无法正常工作。

0 个答案:

没有答案