SKY-P404 · Quality, structure and performance
Unbounded eager ORM query
.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
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
"""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
"""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
- Runs with
--quality, on Python files. - Finds a call to
.all()with no arguments. - 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 namedqueryorselect. - The chain must not contain a limiting call:
limit,first,get,head,one,one_or_none,paginateortake. Afilterorwhereis not a limit. - 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.
"""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.emailWhen 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
querystyle. Django'sModel.objects.all()and SQLAlchemy 2.0'ssession.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.