Skip to main content

SKY-P404 · Quality, structure and performance

Unbounded eager ORM query

Medium

.all() on an ORM query fetches every matching row and builds an object for each one before returning. SKY-P404 reports a SQLAlchemy-style query(...).all() or Model.query.all() call that has no limit or pagination anywhere in the chain.

Languages
Python
Quality gate
counted toward the gate limits (full and diff scans)
Finding type
review
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

Why it matters

A query that returns a few hundred rows in development can return millions in production. The request then holds every row in memory, times out or takes the worker down with it, and the database works hard for rows nobody looks at.

What Skylos flags

app/admin/export.py · PythonReported on line 9
"""Admin export of every customer.""" from sqlalchemy.orm import Session from app.models import Customer  def export_customers(session: Session):    customers = session.query(Customer).filter(Customer.active.is_(True)).all()    return [customer.email for customer in customers]

What Skylos leaves alone

app/admin/customers.py · Python
"""Admin list of customers, one page at a time.""" from sqlalchemy.orm import Session from app.models import Customer PAGE_SIZE = 100  def customer_page(session: Session, page: int):    query = session.query(Customer).filter(Customer.active.is_(True))    return query.order_by(Customer.id).limit(PAGE_SIZE).offset(page * PAGE_SIZE).all()

Why Skylos leaves it alone

The chain contains .limit(...), so at most one page of rows is fetched. Skylos also accepts .first(), .one(), .one_or_none(), .get(), .paginate(), .head() and .take() in the chain.

How Skylos decides

  1. Runs with --quality, on Python files.
  2. Finds a call to .all() with no arguments.
  3. Its receiver must look like an ORM query: somewhere in the chain there is an attribute named query (User.query, db.session.query(...)) or a call to a method named query or select.
  4. The chain must not contain a limiting call: limit, first, get, head, one, one_or_none, paginate or take. A filter or where is not a limit.
  5. Reports MEDIUM on the line of the .all() call.

How to fix it

Page through the rows, or stream them in batches with yield_per, so memory use does not grow with the table.

app/admin/export.py · Python · the fix, against the flagged file
"""Admin export of every customer.""" from sqlalchemy.orm import Session from app.models import Customer  def export_customers(session: Session):Removed:     customers = session.query(Customer).filter(Customer.active.is_(True)).all()Removed:     return [customer.email for customer in customers]Added:     query = session.query(Customer).filter(Customer.active.is_(True))Added:     for customer in query.yield_per(1000):Added:         yield customer.email

When it can be wrong

  • A query on a table that is small by design, such as a list of countries or feature flags, is reported.
  • It only recognises the query style. Django's Model.objects.all() and SQLAlchemy 2.0's session.execute(select(User)).scalars().all() are not reported.

Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.

How to silence it

  • On the reported line:

    Python
    countries = session.query(Country).all()  # skylos: ignore[SKY-P404] 250 rows
  • [tool.skylos] ignore = ["SKY-P404"] turns the rule off for the project. A baseline accepts existing findings, but it records the absolute file path and the line, so an entry stops matching when the checkout moves or the line shifts.

From the Skylos rule handbook, Skylos 5.8.0 (commit 5b880b0). All rules.