简体   繁体   中英

What is the query that is used in when I use square brackets with an sqlalchemy query?

When use the following query:

Foo.query.filter_by(bar="bar")[:20]

Do all the objects that matches the filter get loaded and I only choose the first 20 or are the first 20 objects only get loaded. And if the second, is there a way to load only a specific number of objects?

Thanks in advance guys.

It executes the query and filter, then uses ordinary Python slicing of the result.

If you want it to load only the specific number of records use the slice() method.

Foo.query.filter_by(bar="bar").slice(0,20)

This adds a LIMIT clause to the query, so the database never returns any records outside the range.

The [] is pure python and not related to the query. It means all records (based on the filter are returned and you take only 20). Use limit if you want to return only 20 records from db. See SQLAlchemy query to return only n results?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM