This page is part of the .

    Previous: ORM-Enabled INSERT, UPDATE, and DELETE statements | Next:

    Column Loading Options

    About this Document

    This section presents additional options regarding the loading of columns. The mappings used include columns that would store large string values for which we may want to limit when they are loaded.

    . Some of the examples below will redefine the Book mapper to modify some of the column definitions.

    Column deferral refers to ORM mapped columns that are omitted from a SELECT statement when objects of that type are queried. The general rationale here is performance, in cases where tables have seldom-used columns with potentially large data values, as fully loading these columns on every query may be time and/or memory intensive. SQLAlchemy ORM offers a variety of ways to control the loading of columns when entities are loaded.

    Most examples in this section are illustrating ORM loader options. These are small constructs that are passed to the method of the Select object, which are then consumed by the ORM when the object is compiled into a SQL string.

    The load_only() loader option is the most expedient option to use when loading objects where it is known that only a small handful of columns will be accessed. This option accepts a variable number of class-bound attribute objects indicating those column-mapped attributes that should be loaded, where all other column-mapped attributes outside of the primary key will not be part of the columns fetched . In the example below, the Book class contains columns .title, .summary and .cover_photo. Using we can instruct the ORM to only load the .title and .summary columns up front:

    Above, the SELECT statement has omitted the .cover_photo column and included only .title and .summary, as well as the primary key column .id; the ORM will typically always fetch the primary key columns as these are required to establish the identity for the row.

    Once loaded, the object will normally have lazy loading behavior applied to the remaining unloaded attributes, meaning that when any are first accessed, a SQL statement will be emitted within the current transaction in order to load the value. Below, accessing .cover_photo emits a SELECT statement to load its value:

    1. >>> img_data = books[0].cover_photo
    2. SELECT book.cover_photo AS book_cover_photo
    3. FROM book
    4. WHERE book.id = ?
    5. [...] (1,)

    Lazy loads are always emitted using the to which the object is in the persistent state. If the object is from any Session, the operation fails, raising an exception.

    As an alternative to lazy loading on access, deferred columns may also be configured to raise an informative exception when accessed, regardless of their attachment state. When using the construct, this may be indicated using the load_only.raiseload parameter. See the section for background and examples.

    Tip

    as noted elsewhere, lazy loading is not available when using Asynchronous I/O (asyncio).

    Using load_only() with multiple entities

    load_only() limits itself to the single entity that is referred towards in its list of attributes (passing a list of attributes that span more than a single entity is currently disallowed). In the example below, the given option applies only to the Book entity. The User entity that’s also selected is not affected; within the resulting SELECT statement, all columns for user_account are present, whereas only book.id and book.title are present for the book table:

    1. >>> stmt = select(User, Book).join_from(User, Book).options(load_only(Book.title))
    2. >>> print(stmt)
    3. SELECT user_account.id, user_account.name, user_account.fullname,
    4. book.id AS id_1, book.title
    5. FROM user_account JOIN book ON user_account.id = book.owner_id

    If we wanted to apply load_only() options to both User and Book, we would make use of two separate options:

    1. >>> stmt = (
    2. ... select(User, Book)
    3. ... .join_from(User, Book)
    4. ... .options(load_only(User.name), load_only(Book.title))
    5. ... )
    6. >>> print(stmt)
    7. SELECT user_account.id, user_account.name, book.id AS id_1, book.title
    8. FROM user_account JOIN book ON user_account.id = book.owner_id

    When using relationship loaders to control the loading of related objects, the method of any relationship loader may be used to apply load_only() rules to columns on the sub-entity. In the example below, is used to load the related books collection on each User object. By applying Load.load_only() to the resulting option object, when objects are loaded for the relationship, the SELECT emitted will only refer to the title column in addition to primary key column:

    1. >>> from sqlalchemy.orm import selectinload
    2. >>> stmt = select(User).options(selectinload(User.books).load_only(Book.title))
    3. >>> for user in session.scalars(stmt):
    4. ... print(f"{user.fullname} {[b.title for b in user.books]}")
    5. SELECT user_account.id, user_account.name, user_account.fullname
    6. FROM user_account
    7. [...] ()
    8. SELECT book.owner_id AS book_owner_id, book.id AS book_id, book.title AS book_title
    9. FROM book
    10. WHERE book.owner_id IN (?, ?)
    11. [...] (1, 2)
    12. Spongebob Squarepants ['100 Years of Krabby Patties', 'Sea Catch 22', 'The Sea Grapes of Wrath']
    13. Sandy Cheeks ['A Nut Like No Other', 'Geodesic Domes: A Retrospective', 'Rocketry for Squirrels']

    may also be applied to sub-entities without needing to state the style of loading to use for the relationship itself. If we didn’t want to change the default loading style of User.books but still apply load only rules to Book, we would link using the defaultload() option, which in this case will retain the default relationship loading style of "lazy", and applying our custom rule to the SELECT statement emitted for each User.books collection:

    1. >>> from sqlalchemy.orm import defaultload
    2. >>> stmt = select(User).options(defaultload(User.books).load_only(Book.title))
    3. >>> for user in session.scalars(stmt):
    4. ... print(f"{user.fullname} {[b.title for b in user.books]}")
    5. SELECT user_account.id, user_account.name, user_account.fullname
    6. FROM user_account
    7. [...] ()
    8. SELECT book.id AS book_id, book.title AS book_title
    9. FROM book
    10. WHERE ? = book.owner_id
    11. [...] (1,)
    12. Spongebob Squarepants ['100 Years of Krabby Patties', 'Sea Catch 22', 'The Sea Grapes of Wrath']
    13. SELECT book.id AS book_id, book.title AS book_title
    14. FROM book
    15. WHERE ? = book.owner_id
    16. [...] (2,)
    17. Sandy Cheeks ['A Nut Like No Other', 'Geodesic Domes: A Retrospective', 'Rocketry for Squirrels']

    Using defer() to omit specific columns

    The loader option is a more fine grained alternative to load_only(), which allows a single specific column to be marked as “dont load”. In the example below, is applied directly to the .cover_photo column, leaving the behavior of all other columns unchanged:

    1. >>> from sqlalchemy.orm import defer
    2. >>> stmt = select(Book).where(Book.owner_id == 2).options(defer(Book.cover_photo))
    3. >>> books = session.scalars(stmt).all()
    4. SELECT book.id, book.owner_id, book.title, book.summary
    5. FROM book
    6. WHERE book.owner_id = ?
    7. [...] (2,)
    8. >>> for book in books:
    9. ... print(f"{book.title}: {book.summary}")
    10. A Nut Like No Other: some long summary
    11. Geodesic Domes: A Retrospective: another long summary
    12. Rocketry for Squirrels: yet another summary

    As is the case with load_only(), unloaded columns by default will load themselves when accessed using :

    1. >>> img_data = books[0].cover_photo
    2. SELECT book.cover_photo AS book_cover_photo
    3. FROM book
    4. WHERE book.id = ?
    5. [...] (4,)

    Multiple defer() options may be used in one statement in order to mark several columns as deferred.

    As is the case with , the defer() option also includes the ability to have a deferred attribute raise an exception on access rather than lazy loading. This is illustrated in the section .

    Using raiseload to prevent deferred column loads

    When using the or defer() loader options, attributes marked as deferred on an object have the default behavior that when first accessed, a SELECT statement will be emitted within the current transaction in order to load their value. It is often necessary to prevent this load from occurring, and instead raise an exception when the attribute is accessed, indicating that the need to query the database for this column was not expected. A typical scenario is an operation where objects are loaded with all the columns that are known to be required for the operation to proceed, which are then passed onto a view layer. Any further SQL operations that emit within the view layer should be caught, so that the up-front loading operation can be adjusted to accommodate for that additional data up front, rather than incurring additional lazy loading.

    For this use case the and load_only() options include a boolean parameter , which when set to True will cause the affected attributes to raise on access. In the example below, the deferred column .cover_photo will disallow attribute access:

    1. >>> book = session.scalar(
    2. ... select(Book).options(defer(Book.cover_photo, raiseload=True)).where(Book.id == 4)
    3. ... )
    4. SELECT book.id, book.owner_id, book.title, book.summary
    5. FROM book
    6. WHERE book.id = ?
    7. [...] (4,)
    8. >>> book.cover_photo
    9. Traceback (most recent call last):
    10. ...
    11. sqlalchemy.exc.InvalidRequestError: 'Book.cover_photo' is not available due to raiseload=True

    When using load_only() to name a specific set of non-deferred columns, raiseload behavior may be applied to the remaining columns using the parameter, which will be applied to all deferred attributes:

    1. >>> session.expunge_all()
    2. >>> book = session.scalar(
    3. ... select(Book).options(load_only(Book.title, raiseload=True)).where(Book.id == 5)
    4. ... )
    5. SELECT book.id, book.title
    6. FROM book
    7. WHERE book.id = ?
    8. [...] (5,)
    9. >>> book.summary
    10. Traceback (most recent call last):
    11. ...
    12. sqlalchemy.exc.InvalidRequestError: 'Book.summary' is not available due to raiseload=True

    Note

    It is not yet possible to mix load_only() and options which refer to the same entity together in one statement in order to change the raiseload behavior of certain attributes; currently, doing so will produce undefined loading behavior of attributes.

    See also

    The defer.raiseload feature is the column-level version of the same “raiseload” feature that’s available for relationships. For “raiseload” with relationships, see in the Relationship Loading Techniques section of this guide.

    The functionality of defer() is available as a default behavior for mapped columns, as may be appropriate for columns that should not be loaded unconditionally on every query. To configure, use the parameter of mapped_column(). The example below illustrates a mapping for Book which applies default column deferral to the summary and cover_photo columns:

    1. >>> class Book(Base):
    2. ... __tablename__ = "book"
    3. ... id: Mapped[int] = mapped_column(primary_key=True)
    4. ... owner_id: Mapped[int] = mapped_column(ForeignKey("user_account.id"))
    5. ... title: Mapped[str]
    6. ... summary: Mapped[str] = mapped_column(Text, deferred=True)
    7. ... cover_photo: Mapped[bytes] = mapped_column(LargeBinary, deferred=True)
    8. ...
    9. ... def __repr__(self) -> str:
    10. ... return f"Book(id={self.id!r}, title={self.title!r})"

    Using the above mapping, queries against Book will automatically not include the summary and columns:

    1. >>> book = session.scalar(select(Book).where(Book.id == 2))
    2. SELECT book.id, book.owner_id, book.title
    3. FROM book
    4. WHERE book.id = ?
    5. [...] (2,)

    As is the case with all deferral, the default behavior when deferred attributes on the loaded object are first accessed is that they will their value:

    1. >>> img_data = book.cover_photo
    2. SELECT book.cover_photo AS book_cover_photo
    3. FROM book
    4. WHERE book.id = ?
    5. [...] (2,)

    As is the case with the defer() and loader options, mapper level deferral also includes an option for raiseload behavior to occur, rather than lazy loading, when no other options are present in a statement. This allows a mapping where certain columns will not load by default and will also never load lazily without explicit directives used in a statement. See the section Configuring mapper-level “raiseload” behavior for background on how to configure and use this behavior.

    The deferred() function is the earlier, more general purpose “deferred column” mapping directive that precedes the introduction of the construct in SQLAlchemy.

    deferred() is used when configuring ORM mappers, and accepts arbitrary SQL expressions or objects. As such it’s suitable to be used with non-declarative imperative mappings, passing it to the dictionary:

    1. from sqlalchemy import Blob
    2. from sqlalchemy import Column
    3. from sqlalchemy import ForeignKey
    4. from sqlalchemy import String
    5. from sqlalchemy import Table
    6. from sqlalchemy import Text
    7. from sqlalchemy.orm import registry
    8. mapper_registry = registry()
    9. book_table = Table(
    10. "book",
    11. mapper_registry.metadata,
    12. Column("id", Integer, primary_key=True),
    13. Column("title", String(50)),
    14. Column("summary", Text),
    15. Column("cover_image", Blob),
    16. )
    17. class Book:
    18. pass
    19. mapper_registry.map_imperatively(
    20. Book,
    21. book_table,
    22. properties={
    23. "summary": deferred(book_table.c.summary),
    24. "cover_image": deferred(book_table.c.cover_image),
    25. },
    26. )

    deferred() may also be used in place of when mapped SQL expressions should be loaded on a deferred basis:

    See also

    Using column_property - in the section

    Applying Load, Persistence and Mapping Options for Mapped Table Columns - in the section

    Using undefer() to “eagerly” load deferred columns

    With columns configured on mappings to defer by default, the option will cause any column that is normally deferred to be undeferred, that is, to load up front with all the other columns of the mapping. For example we may apply undefer() to the Book.summary column, which is indicated in the previous mapping as deferred:

    1. >>> from sqlalchemy.orm import undefer
    2. >>> book = session.scalar(select(Book).where(Book.id == 2).options(undefer(Book.summary)))
    3. SELECT book.id, book.owner_id, book.title, book.summary
    4. FROM book
    5. WHERE book.id = ?
    6. [...] (2,)

    The Book.summary column was now eagerly loaded, and may be accessed without additional SQL being emitted:

    1. >>> print(book.summary)
    2. another long summary

    Loading deferred columns in groups

    Normally when a column is mapped with mapped_column(deferred=True), when the deferred attribute is accessed on an object, SQL will be emitted to load only that specific column and no others, even if the mapping has other columns that are also marked as deferred. In the common case that the deferred attribute is part of a group of attributes that should all load at once, rather than emitting SQL for each attribute individually, the mapped_column.deferred_group parameter may be used, which accepts an arbitrary string which will define a common group of columns to be undeferred:

    1. >>> class Book(Base):
    2. ... __tablename__ = "book"
    3. ... id: Mapped[int] = mapped_column(primary_key=True)
    4. ... owner_id: Mapped[int] = mapped_column(ForeignKey("user_account.id"))
    5. ... title: Mapped[str]
    6. ... summary: Mapped[str] = mapped_column(
    7. ... Text, deferred=True, deferred_group="book_attrs"
    8. ... )
    9. ... cover_photo: Mapped[bytes] = mapped_column(
    10. ... LargeBinary, deferred=True, deferred_group="book_attrs"
    11. ... )
    12. ...
    13. ... def __repr__(self) -> str:
    14. ... return f"Book(id={self.id!r}, title={self.title!r})"

    Using the above mapping, accessing either summary or cover_photo will load both columns at once using just one SELECT statement:

    1. >>> book = session.scalar(select(Book).where(Book.id == 2))
    2. SELECT book.id, book.owner_id, book.title
    3. FROM book
    4. WHERE book.id = ?
    5. [...] (2,)
    6. >>> img_data, summary = book.cover_photo, book.summary
    7. SELECT book.summary AS book_summary, book.cover_photo AS book_cover_photo
    8. FROM book
    9. WHERE book.id = ?
    10. [...] (2,)

    If deferred columns are configured with mapped_column.deferred_group as introduced in the preceding section, the entire group may be indicated to load eagerly using the option, passing the string name of the group to be eagerly loaded:

    1. >>> from sqlalchemy.orm import undefer_group
    2. >>> book = session.scalar(
    3. ... select(Book).where(Book.id == 2).options(undefer_group("book_attrs"))
    4. ... )
    5. SELECT book.id, book.owner_id, book.title, book.summary, book.cover_photo
    6. FROM book
    7. WHERE book.id = ?
    8. [...] (2,)

    Both summary and cover_photo are available without additional loads:

    1. >>> img_data, summary = book.cover_photo, book.summary

    Undeferring on wildcards

    Most ORM loader options accept a wildcard expression, indicated by "*", which indicates that the option should be applied to all relevant attributes. If a mapping has a series of deferred columns, all such columns can be undeferred at once, without using a group name, by indicating a wildcard:

    1. >>> book = session.scalar(select(Book).where(Book.id == 3).options(undefer("*")))
    2. SELECT book.id, book.owner_id, book.title, book.summary, book.cover_photo
    3. FROM book
    4. WHERE book.id = ?
    5. [...] (3,)

    Configuring mapper-level “raiseload” behavior

    The “raiseload” behavior first introduced at Using raiseload to prevent deferred column loads may also be applied as a default mapper-level behavior, using the parameter of mapped_column(). When using this parameter, the affected columns will raise on access in all cases unless explicitly “undeferred” using or load_only() at query time:

    1. >>> class Book(Base):
    2. ... __tablename__ = "book"
    3. ... id: Mapped[int] = mapped_column(primary_key=True)
    4. ... owner_id: Mapped[int] = mapped_column(ForeignKey("user_account.id"))
    5. ... title: Mapped[str]
    6. ... summary: Mapped[str] = mapped_column(Text, deferred=True, deferred_raiseload=True)
    7. ... cover_photo: Mapped[bytes] = mapped_column(
    8. ... LargeBinary, deferred=True, deferred_raiseload=True
    9. ... )
    10. ...
    11. ... def __repr__(self) -> str:
    12. ... return f"Book(id={self.id!r}, title={self.title!r})"

    Using the above mapping, the .summary and .cover_photo columns are by default not loadable:

    1. >>> book = session.scalar(select(Book).where(Book.id == 2))
    2. SELECT book.id, book.owner_id, book.title
    3. FROM book
    4. WHERE book.id = ?
    5. [...] (2,)
    6. >>> book.summary
    7. Traceback (most recent call last):
    8. ...
    9. sqlalchemy.exc.InvalidRequestError: 'Book.summary' is not available due to raiseload=True

    Only by overridding their behavior at query time, typically using or undefer_group(), or less commonly , may the attributes be loaded. The example below applies undefer('*') to undefer all attributes, also making use of Populate Existing to refresh the already-loaded object’s loader options:

    1. >>> book = session.scalar(
    2. ... select(Book)
    3. ... .where(Book.id == 2)
    4. ... .options(undefer("*"))
    5. ... .execution_options(populate_existing=True)
    6. ... )
    7. SELECT book.id, book.owner_id, book.title, book.summary, book.cover_photo
    8. FROM book
    9. WHERE book.id = ?
    10. [...] (2,)
    11. >>> book.summary
    12. 'another long summary'

    As discussed Selecting ORM Entities and Attributes and elsewhere, the construct may be used to load arbitrary SQL expressions in a result set. Such as if we wanted to issue a query that loads User objects, but also includes a count of how many books each User owned, we could use func.count(Book.id) to add a “count” column to a query which includes a JOIN to Book as well as a GROUP BY owner id. This will yield Row objects that each contain two entries, one for User and one for func.count(Book.id):

    1. >>> from sqlalchemy import func
    2. >>> stmt = select(User, func.count(Book.id)).join_from(User, Book).group_by(Book.owner_id)
    3. >>> for user, book_count in session.execute(stmt):
    4. SELECT user_account.id, user_account.name, user_account.fullname,
    5. count(book.id) AS count_1
    6. FROM user_account JOIN book ON user_account.id = book.owner_id
    7. GROUP BY book.owner_id
    8. [...] ()
    9. Username: spongebob Number of books: 3
    10. Username: sandy Number of books: 3

    In the above example, the User entity and the “book count” SQL expression are returned separately. However, a popular use case is to produce a query that will yield User objects alone, which can be iterated for example using , where the result of the func.count(Book.id) SQL expression is applied dynamically to each User entity. The end result would be similar to the case where an arbitrary SQL expression were mapped to the class using column_property(), except that the SQL expression can be modified at query time. For this use case SQLAlchemy provides the loader option, which when combined with the mapper level query_expression() directive may produce this result.

    To apply to a query, the mapped class must have pre-configured an ORM mapped attribute using the query_expression() directive; this directive will produce an attribute on the mapped class that is suitable for receiving query-time SQL expressions. Below we add a new attribute User.book_count to User. This ORM mapped attribute is read-only and has no default value; accessing it on a loaded instance will normally produce None:

    1. >>> from sqlalchemy.orm import query_expression
    2. >>> class User(Base):
    3. ... __tablename__ = "user_account"
    4. ... id: Mapped[int] = mapped_column(primary_key=True)
    5. ... name: Mapped[str]
    6. ... fullname: Mapped[Optional[str]]
    7. ... book_count: Mapped[int] = query_expression()
    8. ...
    9. ... return f"User(id={self.id!r}, name={self.name!r}, fullname={self.fullname!r})"

    With the User.book_count attribute configured in our mapping, we may populate it with data from a SQL expression using the loader option to apply a custom SQL expression to each User object as it’s loaded:

    1. >>> from sqlalchemy.orm import with_expression
    2. >>> stmt = (
    3. ... select(User)
    4. ... .join_from(User, Book)
    5. ... .group_by(Book.owner_id)
    6. ... .options(with_expression(User.book_count, func.count(Book.id)))
    7. ... )
    8. >>> for user in session.scalars(stmt):
    9. ... print(f"Username: {user.name} Number of books: {user.book_count}")
    10. SELECT count(book.id) AS count_1, user_account.id, user_account.name,
    11. user_account.fullname
    12. FROM user_account JOIN book ON user_account.id = book.owner_id
    13. GROUP BY book.owner_id
    14. [...] ()
    15. Username: spongebob Number of books: 3
    16. Username: sandy Number of books: 3

    The query_expression() mapping has these caveats:

    • On an object where were not used to populate the attribute, the attribute on an object instance will have the value None, unless on the mapping the query_expression.default_expr parameter is set to a default SQL expression.

    • The value does not populate on an object that is already loaded, unless Populate Existing is used. The example below will not work, as the A object is already loaded:

      To ensure the attribute is re-loaded on an existing object, use the execution option to ensure all columns are re-populated:

      1. obj = session.scalars(
      2. select(A)
      3. .options(with_expression(A.expr, some_expr))
      4. .execution_options(populate_existing=True)
      5. ).first()
    • The with_expression() SQL expression is lost when the object is expired. Once the object is expired, either via or via the expire_on_commit behavior of Session.commit(), the SQL expression and its value is no longer associated with the attribute and will return None on subsequent access.

    • , as an object loading option, only takes effect on the outermost part of a query and only for a query against a full entity, and not for arbitrary column selects, within subqueries, or the elements of a compound statement such as a UNION. See the next section Using with_expression() with UNIONs, other subqueries for an example.

    • The mapped attribute cannot be applied to other parts of the query, such as the WHERE clause, the ORDER BY clause, and make use of the ad-hoc expression; that is, this won’t work:

      1. # can't refer to A.expr elsewhere in the query
      2. stmt = (
      3. select(A)
      4. .options(with_expression(A.expr, A.x + A.y))
      5. .filter(A.expr > 5)
      6. .order_by(A.expr)
      7. )

      The A.expr expression will resolve to NULL in the above WHERE clause and ORDER BY clause. To use the expression throughout the query, assign to a variable and use that:

      1. # assign desired expression up front, then refer to that in
      2. # the query
      3. a_expr = A.x + A.y
      4. stmt = (
      5. select(A)
      6. .options(with_expression(A.expr, a_expr))
      7. .filter(a_expr > 5)
      8. .order_by(a_expr)
      9. )

    See also

    The option is a special option used to apply SQL expressions to mapped classes dynamically at query time. For ordinary fixed SQL expressions configured on mappers, see the section SQL Expressions as Mapped Attributes.

    The with_expression() construct is an ORM loader option, and as such may only be applied to the outermost level of a SELECT statement which is to load a particular ORM entity. It does not have any effect if used inside of a that will then be used as a subquery or as an element within a compound statement such as a UNION.

    In order to use arbitrary SQL expressions in subqueries, normal Core-style means of adding expressions should be used. To assemble a subquery-derived expression onto the ORM entity’s query_expression() attributes, is used at the top layer of ORM object loading, referencing the SQL expression within the subquery.

    In the example below, two select() constructs are used against the ORM entity A with an additional SQL expression labeled in expr, and combined using . Then, at the topmost layer, the A entity is SELECTed from this UNION, using the querying technique described at Selecting Entities from UNIONs and other set operations, adding an option with to extract this SQL expression onto newly loaded instances of A:

    1. >>> from sqlalchemy import union_all
    2. >>> s1 = (
    3. ... select(User, func.count(Book.id).label("book_count"))
    4. ... .join_from(User, Book)
    5. ... .where(User.name == "spongebob")
    6. ... )
    7. >>> s2 = (
    8. ... select(User, func.count(Book.id).label("book_count"))
    9. ... .join_from(User, Book)
    10. ... .where(User.name == "sandy")
    11. ... )
    12. >>> union_stmt = union_all(s1, s2)
    13. >>> orm_stmt = (
    14. ... select(User)
    15. ... .from_statement(union_stmt)
    16. ... .options(with_expression(User.book_count, union_stmt.c.book_count))
    17. ... )
    18. >>> for user in session.scalars(orm_stmt):
    19. ... print(f"Username: {user.name} Number of books: {user.book_count}")
    20. SELECT user_account.id, user_account.name, user_account.fullname, count(book.id) AS book_count
    21. FROM user_account JOIN book ON user_account.id = book.owner_id
    22. WHERE user_account.name = ?
    23. UNION ALL
    24. SELECT user_account.id, user_account.name, user_account.fullname, count(book.id) AS book_count
    25. FROM user_account JOIN book ON user_account.id = book.owner_id
    26. WHERE user_account.name = ?
    27. [...] ('spongebob', 'sandy')
    28. Username: spongebob Number of books: 3
    29. Username: sandy Number of books: 3

    function sqlalchemy.orm.defer(key: Union[str, [Any]], *addl_attrs: Union[str, QueryableAttribute[Any]], raiseload: bool = False) → _AbstractLoad

    Indicate that the given column-oriented attribute should be deferred, e.g. not loaded until accessed.

    This function is part of the interface and supports both method-chained and standalone operation.

    e.g.:

    1. from sqlalchemy.orm import defer
    2. session.query(MyClass).options(
    3. defer(MyClass.attribute_one),
    4. defer(MyClass.attribute_two)
    5. )

    To specify a deferred load of an attribute on a related class, the path can be specified one token at a time, specifying the loading style for each link along the chain. To leave the loading style for a link unchanged, use defaultload():

    1. session.query(MyClass).options(
    2. defaultload(MyClass.someattr).defer(RelatedClass.some_column)
    3. )

    Multiple deferral options related to a relationship can be bundled at once using :

    1. session.query(MyClass).options(
    2. defaultload(MyClass.someattr).options(
    3. defer(RelatedClass.some_column),
    4. defer(RelatedClass.some_other_column),
    5. defer(RelatedClass.another_column)
    6. )
    7. )
    • Parameters:

      • key – Attribute to be deferred.

      • raiseload – raise InvalidRequestError rather than lazy loading a value when the deferred attribute is accessed. Used to prevent unwanted SQL from being emitted.

    New in version 1.4.

    See also

    - in the ORM Querying Guide

    undefer()

    function sqlalchemy.orm.deferred(column: _ORMColumnExprArgument[_T], *additional_columns: _ORMColumnExprArgument[Any], group: Optional[str] = None, raiseload: bool = False, comparator_factory: Optional[Type[[_T]]] = None, init: Union[_NoArg, bool] = _NoArg.NO_ARG, repr: Union[_NoArg, bool] = _NoArg.NO_ARG, default: Optional[Any] = _NoArg.NO_ARG, default_factory: Union[_NoArg, Callable[[], _T]] = _NoArg.NO_ARG, compare: Union[_NoArg, bool] = _NoArg.NO_ARG, kw_only: Union[_NoArg, bool] = _NoArg.NO_ARG, active_history: bool = False, expire_on_flush: bool = True, info: Optional[_InfoType] = None, doc: Optional[str] = None) → ColumnProperty[_T]

    Indicate a column-based mapped attribute that by default will not load unless accessed.

    When using , the same functionality as that of deferred() construct is provided by using the parameter.

    • Parameters:

      • *columns – columns to be mapped. This is typically a single Column object, however a collection is supported in order to support multiple columns mapped under the same attribute.

      • raiseload

        boolean, if True, indicates an exception should be raised if the load operation is to take place.

        New in version 1.4.

    Additional arguments are the same as that of .

    See also

    Using deferred() for imperative mappers, mapped SQL expressions

    function sqlalchemy.orm.query_expression(default_expr: _ORMColumnExprArgument[_T] = <sqlalchemy.sql.elements.Null object>, *, repr: Union[_NoArg, bool] = _NoArg.NO_ARG, expire_on_flush: bool = True, info: Optional[_InfoType] = None, doc: Optional[str] = None) → [_T]

    • Parameters:

      default_expr – Optional SQL expression object that will be used in all cases if not assigned later with with_expression().

    New in version 1.2.

    See also

    - background and usage examples

    function sqlalchemy.orm.load_only(*attrs: Union[str, QueryableAttribute[Any]], raiseload: bool = False) → _AbstractLoad

    Indicate that for a particular entity, only the given list of column-based attribute names should be loaded; all others will be deferred.

    This function is part of the interface and supports both method-chained and standalone operation.

    Example - given a class User, load only the name and fullname attributes:

    1. session.query(User).options(load_only(User.name, User.fullname))

    Example - given a relationship User.addresses -> Address, specify subquery loading for the User.addresses collection, but on each Address object load only the email_address attribute:

    1. session.query(User).options(
    2. subqueryload(User.addresses).load_only(Address.email_address)
    3. )

    For a statement that has multiple entities, the lead entity can be specifically referred to using the Load constructor:

    1. stmt = select(User, Address).join(User.addresses).options(
    2. Load(User).load_only(User.name, User.fullname),
    3. Load(Address).load_only(Address.email_address)
    4. )
    • Parameters:

      • *attrs – Attributes to be loaded, all others will be deferred.

      • raiseload

        raise rather than lazy loading a value when a deferred attribute is accessed. Used to prevent unwanted SQL from being emitted.

        New in version 2.0.

    See also

    Limiting which Columns Load with Column Deferral - in the

    • Parameters:

      • *attrs – Attributes to be loaded, all others will be deferred.

      • raiseload

        raise InvalidRequestError rather than lazy loading a value when a deferred attribute is accessed. Used to prevent unwanted SQL from being emitted.

        New in version 2.0.

    function sqlalchemy.orm.undefer(key: Union[str, [Any]], *addl_attrs: Union[str, QueryableAttribute[Any]]) → _AbstractLoad

    Indicate that the given column-oriented attribute should be undeferred, e.g. specified within the SELECT statement of the entity as a whole.

    The column being undeferred is typically set up on the mapping as a attribute.

    This function is part of the Load interface and supports both method-chained and standalone operation.

    Examples:

    1. # undefer two columns
    2. session.query(MyClass).options(
    3. undefer(MyClass.col1), undefer(MyClass.col2)
    4. )
    5. # undefer all columns specific to a single class using Load + *
    6. session.query(MyClass, MyOtherClass).options(
    7. Load(MyClass).undefer("*"))
    8. # undefer a column on a related object
    9. session.query(MyClass).options(
    10. defaultload(MyClass.items).undefer(MyClass.text))
    • Parameters:

      key – Attribute to be undeferred.

    See also

    - in the ORM Querying Guide

    undefer_group()

    function sqlalchemy.orm.undefer_group(name: str) → _AbstractLoad

    Indicate that columns within the given deferred group name should be undeferred.

    The columns being undeferred are set up on the mapping as attributes and include a “group” name.

    E.g:

    1. session.query(MyClass).options(undefer_group("large_attrs"))

    To undefer a group of attributes on a related entity, the path can be spelled out using relationship loader options, such as defaultload():

    1. defaultload("someattr").undefer_group("large_attrs"))

    See also

    - in the ORM Querying Guide

    undefer()

    function sqlalchemy.orm.with_expression(key: _AttrType, expression: _ColumnExpressionArgument[Any]) → _AbstractLoad

    Apply an ad-hoc SQL expression to a “deferred expression” attribute.

    This option is used in conjunction with the mapper-level construct that indicates an attribute which should be the target of an ad-hoc SQL expression.

    E.g.:

    New in version 1.2.

    • Parameters:

      • key – Attribute to be populated

    See also

    Loading Arbitrary SQL Expressions onto Objects - background and usage examples

    Next Query Guide Section: