joshualan
joshualan

Reputation: 2140

When does the database actually get queried in SQLAlchemy?

Let's say you have this code:

products = session.query(Products)
query = products.filter(Products.productVendor == 'Classic Metal Creations')

for q in query:
    print q

When is the database queried? How many times is the database queried here?

So for example. when we do a filter() on products, are we just performing that operation client-side on the result set that we got from the original query?

Upvotes: 0

Views: 28

Answers (1)

univerio
univerio

Reputation: 20538

The database is actually queried when you iterate through query, i.e. the line for q in query. This includes calling all(), first(), scalar(), etc. on the query. filter() only generatively modifies the query to include the filter.

Upvotes: 1

Related Questions