class ColumnCollection(Generic[
Known subclasses: sqlalchemy.sql.base.DedupeColumnCollection
, sqlalchemy.sql.base.ReadOnlyColumnCollection
Collection of :class:`_expression.ColumnElement` instances, typically for :class:`_sql.FromClause` objects. The :class:`_sql.ColumnCollection` object is most commonly available as the :attr:`_schema.Table.c` or :attr:`_schema.Table.columns` collection on the :class:`_schema.Table` object, introduced at :ref:`metadata_tables_and_columns`. The :class:`_expression.ColumnCollection` has both mapping- and sequence- like behaviors. A :class:`_expression.ColumnCollection` usually stores :class:`_schema.Column` objects, which are then accessible both via mapping style access as well as attribute access style. To access :class:`_schema.Column` objects using ordinary attribute-style access, specify the name like any other object attribute, such as below a column named ``employee_name`` is accessed:: >>> employee_table.c.employee_name To access columns that have names with special characters or spaces, index-style access is used, such as below which illustrates a column named ``employee ' payment`` is accessed:: >>> employee_table.c["employee ' payment"] As the :class:`_sql.ColumnCollection` object provides a Python dictionary interface, common dictionary method names like :meth:`_sql.ColumnCollection.keys`, :meth:`_sql.ColumnCollection.values`, and :meth:`_sql.ColumnCollection.items` are available, which means that database columns that are keyed under these names also need to use indexed access:: >>> employee_table.c["values"] The name for which a :class:`_schema.Column` would be present is normally that of the :paramref:`_schema.Column.key` parameter. In some contexts, such as a :class:`_sql.Select` object that uses a label style set using the :meth:`_sql.Select.set_label_style` method, a column of a certain key may instead be represented under a particular label name such as ``tablename_columnname``:: >>> from sqlalchemy import select, column, table >>> from sqlalchemy import LABEL_STYLE_TABLENAME_PLUS_COL >>> t = table("t", column("c")) >>> stmt = select(t).set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) >>> subq = stmt.subquery() >>> subq.c.t_c <sqlalchemy.sql.elements.ColumnClause at 0x7f59dcf04fa0; t_c> :class:`.ColumnCollection` also indexes the columns in order and allows them to be accessible by their integer position:: >>> cc[0] Column('x', Integer(), table=None) >>> cc[1] Column('y', Integer(), table=None) .. versionadded:: 1.4 :class:`_expression.ColumnCollection` allows integer-based index access to the collection. Iterating the collection yields the column expressions in order:: >>> list(cc) [Column('x', Integer(), table=None), Column('y', Integer(), table=None)] The base :class:`_expression.ColumnCollection` object can store duplicates, which can mean either two columns with the same key, in which case the column returned by key access is **arbitrary**:: >>> x1, x2 = Column('x', Integer), Column('x', Integer) >>> cc = ColumnCollection(columns=[(x1.name, x1), (x2.name, x2)]) >>> list(cc) [Column('x', Integer(), table=None), Column('x', Integer(), table=None)] >>> cc['x'] is x1 False >>> cc['x'] is x2 True Or it can also mean the same column multiple times. These cases are supported as :class:`_expression.ColumnCollection` is used to represent the columns in a SELECT statement which may include duplicates. A special subclass :class:`.DedupeColumnCollection` exists which instead maintains SQLAlchemy's older behavior of not allowing duplicates; this collection is used for schema level objects like :class:`_schema.Table` and :class:`.PrimaryKeyConstraint` where this deduping is helpful. The :class:`.DedupeColumnCollection` class also has additional mutation methods as the schema constructs have more use cases that require removal and replacement of columns. .. versionchanged:: 1.4 :class:`_expression.ColumnCollection` now stores duplicate column keys as well as the same column in multiple positions. The :class:`.DedupeColumnCollection` class is added to maintain the former behavior in those cases where deduplication as well as additional replace/remove operations are needed.
Method | __bool__ |
Undocumented |
Method | __clause |
Undocumented |
Method | __contains__ |
Undocumented |
Method | __delitem__ |
Undocumented |
Method | __eq__ |
Undocumented |
Method | __getattr__ |
Undocumented |
Method | __getitem__ |
Undocumented |
Method | __getstate__ |
Undocumented |
Method | __init__ |
Undocumented |
Method | __iter__ |
Undocumented |
Method | __len__ |
Undocumented |
Method | __setattr__ |
Undocumented |
Method | __setitem__ |
Undocumented |
Method | __setstate__ |
Undocumented |
Method | __str__ |
Undocumented |
Method | add |
Add a column to this :class:`_sql.ColumnCollection`. |
Method | as |
Return a "read only" form of this :class:`_sql.ColumnCollection`. |
Method | clear |
Dictionary clear() is not implemented for :class:`_sql.ColumnCollection`. |
Method | compare |
Compare this :class:`_expression.ColumnCollection` to another based on the names of the keys |
Method | contains |
Checks if a column object exists in this collection |
Method | corresponding |
Given a :class:`_expression.ColumnElement`, return the exported :class:`_expression.ColumnElement` object from this :class:`_expression.ColumnCollection` which corresponds to that original :class:`_expression... |
Method | get |
Get a :class:`_sql.ColumnClause` or :class:`_schema.Column` object based on a string key name from this :class:`_expression.ColumnCollection`. |
Method | items |
Return a sequence of (key, column) tuples for all columns in this collection each consisting of a string key name and a :class:`_sql.ColumnClause` or :class:`_schema.Column` object. |
Method | keys |
Return a sequence of string key names for all columns in this collection. |
Method | remove |
Undocumented |
Method | update |
Dictionary update() is not implemented for :class:`_sql.ColumnCollection`. |
Method | values |
Return a sequence of :class:`_sql.ColumnClause` or :class:`_schema.Column` objects for all columns in this collection. |
Class Variable | __hash__ |
Undocumented |
Class Variable | __slots__ |
Undocumented |
Method | _init |
populate the "proxy index", if empty. |
Method | _initial |
Undocumented |
Method | _populate |
populate from an iterator of (key, column) |
Class Variable | _collection |
Undocumented |
Class Variable | _colset |
Undocumented |
Class Variable | _index |
Undocumented |
Class Variable | _proxy |
Undocumented |
Property | _all |
Undocumented |
def __clause_element__(self) ->
ClauseList
:
(source)
¶
Undocumented
@overload
Tuple[ Union[ str, int], ...]
) -> ReadOnlyColumnCollection[ _COLKEY, _COL_co]
:Undocumented
sqlalchemy.sql.base.DedupeColumnCollection
, sqlalchemy.sql.base.ReadOnlyColumnCollection
Add a column to this :class:`_sql.ColumnCollection`. .. note:: This method is **not normally used by user-facing code**, as the :class:`_sql.ColumnCollection` is usually part of an existing object such as a :class:`_schema.Table`. To add a :class:`_schema.Column` to an existing :class:`_schema.Table` object, use the :meth:`_schema.Table.append_column` method.
_COL
, require_embedded: bool
= False) -> Optional[ Union[ _COL, _COL_co]]
:
(source)
¶
Given a :class:`_expression.ColumnElement`, return the exported :class:`_expression.ColumnElement` object from this :class:`_expression.ColumnCollection` which corresponds to that original :class:`_expression.ColumnElement` via a common ancestor column. :param column: the target :class:`_expression.ColumnElement` to be matched. :param require_embedded: only return corresponding columns for the given :class:`_expression.ColumnElement`, if the given :class:`_expression.ColumnElement` is actually present within a sub-element of this :class:`_expression.Selectable`. Normally the column will match if it merely shares a common ancestor with one of the exported columns of this :class:`_expression.Selectable`. .. seealso:: :meth:`_expression.Selectable.corresponding_column` - invokes this method against the collection returned by :attr:`_expression.Selectable.exported_columns`. .. versionchanged:: 1.4 the implementation for ``corresponding_column`` was moved onto the :class:`_expression.ColumnCollection` itself.
Get a :class:`_sql.ColumnClause` or :class:`_schema.Column` object based on a string key name from this :class:`_expression.ColumnCollection`.
Return a sequence of (key, column) tuples for all columns in this collection each consisting of a string key name and a :class:`_sql.ColumnClause` or :class:`_schema.Column` object.
sqlalchemy.sql.base.DedupeColumnCollection
, sqlalchemy.sql.base.ReadOnlyColumnCollection
Undocumented
Return a sequence of :class:`_sql.ColumnClause` or :class:`_schema.Column` objects for all columns in this collection.
populate the "proxy index", if empty. proxy index is added in 2.0 to provide more efficient operation for the corresponding_column() method. For reasons of both time to construct new .c collections as well as memory conservation for large numbers of large .c collections, the proxy_index is only filled if corresponding_column() is called. once filled it stays that way, and new _ColumnMetrics objects created after that point will populate it with new data. Note this case would be unusual, if not nonexistent, as it means a .c collection is being mutated after corresponding_column() were used, however it is tested in test/base/test_utils.py.