Вопрос

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
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top