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,
)
| 6749 | |
| 6750 | @HasMemoized_ro_memoized_attribute |
| 6751 | def selected_columns( |
| 6752 | self, |
| 6753 | ) -> ColumnCollection[str, ColumnElement[Any]]: |
| 6754 | """A :class:`_expression.ColumnCollection` |
| 6755 | representing the columns that |
| 6756 | this SELECT statement or similar construct returns in its result set, |
| 6757 | not including :class:`_sql.TextClause` constructs. |
| 6758 | |
| 6759 | This collection differs from the :attr:`_expression.FromClause.columns` |
| 6760 | collection of a :class:`_expression.FromClause` in that the columns |
| 6761 | within this collection cannot be directly nested inside another SELECT |
| 6762 | statement; a subquery must be applied first which provides for the |
| 6763 | necessary parenthesization required by SQL. |
| 6764 | |
| 6765 | For a :func:`_expression.select` construct, the collection here is |
| 6766 | exactly what would be rendered inside the "SELECT" statement, and the |
| 6767 | :class:`_expression.ColumnElement` objects are directly present as they |
| 6768 | were given, e.g.:: |
| 6769 | |
| 6770 | col1 = column("q", Integer) |
| 6771 | col2 = column("p", Integer) |
| 6772 | stmt = select(col1, col2) |
| 6773 | |
| 6774 | Above, ``stmt.selected_columns`` would be a collection that contains |
| 6775 | the ``col1`` and ``col2`` objects directly. For a statement that is |
| 6776 | against a :class:`_schema.Table` or other |
| 6777 | :class:`_expression.FromClause`, the collection will use the |
| 6778 | :class:`_expression.ColumnElement` objects that are in the |
| 6779 | :attr:`_expression.FromClause.c` collection of the from element. |
| 6780 | |
| 6781 | A use case for the :attr:`_sql.Select.selected_columns` collection is |
| 6782 | to allow the existing columns to be referenced when adding additional |
| 6783 | criteria, e.g.:: |
| 6784 | |
| 6785 | def filter_on_id(my_select, id): |
| 6786 | return my_select.where(my_select.selected_columns["id"] == id) |
| 6787 | |
| 6788 | |
| 6789 | stmt = select(MyModel) |
| 6790 | |
| 6791 | # adds "WHERE id=:param" to the statement |
| 6792 | stmt = filter_on_id(stmt, 42) |
| 6793 | |
| 6794 | .. note:: |
| 6795 | |
| 6796 | The :attr:`_sql.Select.selected_columns` collection does not |
| 6797 | include expressions established in the columns clause using the |
| 6798 | :func:`_sql.text` construct; these are silently omitted from the |
| 6799 | collection. To use plain textual column expressions inside of a |
| 6800 | :class:`_sql.Select` construct, use the :func:`_sql.literal_column` |
| 6801 | construct. |
| 6802 | |
| 6803 | |
| 6804 | .. versionadded:: 1.4 |
| 6805 | |
| 6806 | """ |
| 6807 | |
| 6808 | # compare to SelectState._generate_columns_plus_names, which |
nothing calls this directly
no test coverage detected