Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLAlchemy SessionTransaction has query attribute?

I'm pretty new in Python and SQLAlchemy, so basically I changed from:

session = Session()
session.query(SOMETHING_HERE).filter()

to

session = Session()
nested_session = session.begin_nested()
nested_session.query(SOMETHING_HERE).filter()

in order to use SAVEPOINTS. But I'm getting the error:

AttributeError: 'SessionTransaction' object has no attribute 'query'

I did a deep dive in the documentation, like these:

  • https://docs.sqlalchemy.org/en/13/orm/session_api.html#sqlalchemy.orm.session.SessionTransaction

  • https://docs.sqlalchemy.org/en/13/orm/session_transaction.html

But it did not help me.

Questions: Am I able to use begin_nested to run query with filter like that or I need to change the approach? Is this a version issue (sql alchemy for example)?

like image 976
BrTkCa Avatar asked Mar 31 '26 04:03

BrTkCa


1 Answers

This was originally posted as a comment.

You don't need to reassign the result of session.begin_nested() to anything, you can just do: session.begin_nested() and then proceed with session.query(SOMETHING_HERE).filter()

Reference for this assertion: https://docs.sqlalchemy.org/en/13/orm/session_transaction.html#using-savepoint

like image 197
mechanical_meat Avatar answered Apr 02 '26 19:04

mechanical_meat