Q: When does a SQL Alchemy query actually make a query to the database? I am looking at some code that conditionally creates a query object and then looks at the results of the query. For example:
query = self.session.query(Person).filter(Person.last_name == "Jones") #line 1
#some other logic
query = query.filter(Person.title == "VP Sales") #line 3
#some other logic
query.first() #line 5

When would the query actually hit the database? Ideally, the query would only run when I need to use the results of the query (ie. line 5).
I tried to look in the SQL Alchemy docs (https://docs.sqlalchemy.org/en/13/orm/query.html) but was not able to find this information. 

A: In this case, it would hit the database when calling .first(), the rest of the lines are simply formatting a query object.
That being said, the query would also get fired as soon as you would iterate over your query object or if you would call .all() on it.
query = self.session.query(Person).filter(Person.last_name == "Jones") 
query = query.filter(Person.title == "VP Sales") #line 3

for person in query:    # hit db here
    print(peron)

# OR

persons = query.all()   # hit db here

# OR

persons = list(query)   # hit db here (note that this has the same effect as calling .all())

