MCPcopy Create free account
hub / github.com/sqlalchemy/sqlalchemy / selected_columns

Method selected_columns

lib/sqlalchemy/sql/selectable.py:6464–6537  ·  view source on GitHub ↗

A :class:`_expression.ColumnCollection` representing the columns that this SELECT statement or similar construct returns in its result set, not including :class:`_sql.TextClause` constructs. This collection differs from the :attr:`_expression.FromClause.columns`

(
        self,
    )

Source from the content-addressed store, hash-verified

6462
6463 @HasMemoized_ro_memoized_attribute
6464 def selected_columns(
6465 self,
6466 ) -> ColumnCollection[str, ColumnElement[Any]]:
6467 """A :class:`_expression.ColumnCollection`
6468 representing the columns that
6469 this SELECT statement or similar construct returns in its result set,
6470 not including :class:`_sql.TextClause` constructs.
6471
6472 This collection differs from the :attr:`_expression.FromClause.columns`
6473 collection of a :class:`_expression.FromClause` in that the columns
6474 within this collection cannot be directly nested inside another SELECT
6475 statement; a subquery must be applied first which provides for the
6476 necessary parenthesization required by SQL.
6477
6478 For a :func:`_expression.select` construct, the collection here is
6479 exactly what would be rendered inside the "SELECT" statement, and the
6480 :class:`_expression.ColumnElement` objects are directly present as they
6481 were given, e.g.::
6482
6483 col1 = column("q", Integer)
6484 col2 = column("p", Integer)
6485 stmt = select(col1, col2)
6486
6487 Above, ``stmt.selected_columns`` would be a collection that contains
6488 the ``col1`` and ``col2`` objects directly. For a statement that is
6489 against a :class:`_schema.Table` or other
6490 :class:`_expression.FromClause`, the collection will use the
6491 :class:`_expression.ColumnElement` objects that are in the
6492 :attr:`_expression.FromClause.c` collection of the from element.
6493
6494 A use case for the :attr:`_sql.Select.selected_columns` collection is
6495 to allow the existing columns to be referenced when adding additional
6496 criteria, e.g.::
6497
6498 def filter_on_id(my_select, id):
6499 return my_select.where(my_select.selected_columns["id"] == id)
6500
6501
6502 stmt = select(MyModel)
6503
6504 # adds "WHERE id=:param" to the statement
6505 stmt = filter_on_id(stmt, 42)
6506
6507 .. note::
6508
6509 The :attr:`_sql.Select.selected_columns` collection does not
6510 include expressions established in the columns clause using the
6511 :func:`_sql.text` construct; these are silently omitted from the
6512 collection. To use plain textual column expressions inside of a
6513 :class:`_sql.Select` construct, use the :func:`_sql.literal_column`
6514 construct.
6515
6516
6517 .. versionadded:: 1.4
6518
6519 """
6520
6521 # compare to SelectState._generate_columns_plus_names, which

Callers

nothing calls this directly

Calls 6

as_readonlyMethod · 0.95
castFunction · 0.85
ColumnCollectionClass · 0.85
convClass · 0.85
is_column_elementFunction · 0.85

Tested by

no test coverage detected