1

I am using SQL Alchemy ORM and have some classes/tables each of which may have some custom queries. Let's say, I want to add to table Fruit the filtering possibility called with_seed giving me only fruits with seeds, and to table Cutlery the filtering method is_sharp giving me only sharp cutlery. I want to define these filters as extensions to the Query object, and I want to use them in the same transaction:

def delete_sharp_cutlery_and_seedy_fruits(session_factory):
  session = session_factory()
  session.query(Fruit).with_seed().delete(synchronize_session='fetch')
  session.query(Cutlery).is_sharp().delete(synchronize_session='fetch')
  session.commit()

Is this possible?


This is related to the question here. But the solution there requires different sessions to be created for the different query classes.

Community
  • 1
  • 1
Mahdi
  • 1,778
  • 1
  • 21
  • 35

1 Answers1

0

You can pass session to Query constructor

CustomQuery(entities=[Fruit], session=session).with_seed().delete(synchronize_session='fetch')
r-m-n
  • 14,192
  • 4
  • 69
  • 68