简体   繁体   中英

SqlAlchemy and PostgreSql datetime update

I have a PostgreSql table in which I want to update an attribute of type timestamp with timezone (I tried also without timestamp but it does not work). I'm using SqlAlchemy session for that purpose.

I fetch an existing record, and update it with a current timestamp:

from model import Table
from dbconf import session

t=session.query(Table).filter(Table.id==1).first()
t.available=datetime.now()
session.add(t)
session.commit()

After this command nothing change in the database. What am I doing wrong?

I can assume that you have model of this table, you should add there new update method like this:

class table(Base):
    __tablename__ = 'table'

    id = Column(Integer, primary_key=True)
    available = Column(DateTime)
    asd = Column(Unicode(255))


    def update(self, available=None, asd = None):  #etc.
        if available:
            self.available = available
        if asd:
            self.asd = asd

and updating happens then like this:

import transaction
with transaction.manager:
    t=session.query(Table).filter(Table.id==1).first() #search the object what you want to update
    t.update(available=datetime.now()) #you can update only one or several cell like this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM