2

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?

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

2 Comments

looks like thats it. but update should work without it too, only reason to use transaction is that, when something goes wrong, every changes made to db inside of transaction.manager, is rollbacked. So it's extra protection.
There shouldn't be any need for this. Either doing it where he currently tries to update to it and doing it here are going to be one in the same :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.