Skip to content

Eager Fetch Type On Association (ARCH2060)

Key python.orm.eager-fetch-type-on-association · Severity warning · Category orm

Declaring a relation as always-eager at the mapping level over-fetches on every query of the owning entity. Decide eager loading per query instead.

class Author(Base):
books = relationship("Book", lazy="joined") # fetches books on every Author query
class Author(Base):
books = relationship("Book", lazy="select") # only when needed
session.query(Author).options(joinedload(Author.books)) # opt in per query

Leave the relation lazy by default; opt into eager loading on the specific query that needs it, not in the mapping.