SQLAlchemy query to return only n results?

Sqlalchemy

Sqlalchemy Problem Overview


I have been googling and reading through the SQLAlchemy documentation but haven't found what I am looking for.

I am looking for a function in SQLAlchemy that limits the number of results returned by a query to a certain number, for example: 5? Something like first() or all().

Sqlalchemy Solutions


Solution 1 - Sqlalchemy

for sqlalchemy >= 1.0.13 Use the limit method.

query(Model).filter(something).limit(5).all()

Solution 2 - Sqlalchemy

Alternative syntax

query.(Model).filter(something)[:5].all()

Solution 3 - Sqlalchemy

In my case it works like

def get_members():
    m = Member.query[:30]
    return m

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionXarView Question on Stackoverflow
Solution 1 - SqlalchemyjkmaccView Answer on Stackoverflow
Solution 2 - SqlalchemymcolakView Answer on Stackoverflow
Solution 3 - SqlalchemyAnar SalimkhanovView Answer on Stackoverflow