8

UserTable is:

  • id (INT)

  • name (STR)

  • last_login (DATETIME)

Serving a web page request i have a user id in hand and I only wish to update the last_login field to 'now'.

It seems to me that there are 2 ways:

  1. issue a direct SQL using db_engine (losing the mapper)

  2. OR query the user first and then update the object

Both work fine but look quite disgusting in code.

Is anyone aware of a more elegant way of doing an update-with-no-query using sqlalchemy? Is there another ORM who has got this right?

Thanks

1 Answer 1

23

Assuming you have a mapper UserTable in place:

DBSession.query(UserTable).filter_by(id = user_id).\
    update({"last_login":datetime.datetime.now()}, synchronize_session=False)

Additional parameters in the docs.

Sign up to request clarification or add additional context in comments.

1 Comment

Actual link to the docs: docs.sqlalchemy.org/en/latest/orm/…

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.