class documentation

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_element__ 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_readonly 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_column Checks if a column object exists in this collection
Method corresponding_column 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_proxy_index populate the "proxy index", if empty.
Method _initial_populate Undocumented
Method _populate_separate_keys populate from an iterator of (key, column)
Class Variable _collection Undocumented
Class Variable _colset Undocumented
Class Variable _index Undocumented
Class Variable _proxy_index Undocumented
Property _all_columns Undocumented
def __bool__(self) -> bool: (source)

Undocumented

@util.preload_module('sqlalchemy.sql.elements')
def __clause_element__(self) -> ClauseList: (source)

Undocumented

def __contains__(self, key: str) -> bool: (source)

Undocumented

def __delitem__(self, key: str) -> NoReturn: (source)

Undocumented

def __eq__(self, other: Any) -> bool: (source)

Undocumented

def __getattr__(self, key: str) -> _COL_co: (source)

Undocumented

@overload
def __getitem__(self, key: Union[str, int]) -> _COL_co:
@overload
def __getitem__(self, key: Tuple[Union[str, int], ...]) -> ReadOnlyColumnCollection[_COLKEY, _COL_co]:
(source)

Undocumented

def __getstate__(self) -> Dict[str, Any]: (source)
def __init__(self, columns: Optional[Iterable[Tuple[_COLKEY, _COL_co]]] = None): (source)
def __iter__(self) -> Iterator[_COL_co]: (source)

Undocumented

def __len__(self) -> int: (source)

Undocumented

def __setattr__(self, key: str, obj: Any) -> NoReturn: (source)

Undocumented

def __setitem__(self, key: str, value: Any) -> NoReturn: (source)

Undocumented

def __setstate__(self, state: Dict[str, Any]): (source)
def __str__(self) -> str: (source)

Undocumented

def add(self, column: ColumnElement[Any], key: Optional[_COLKEY] = None): (source)

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.

Return a "read only" form of this :class:`_sql.ColumnCollection`.

def clear(self) -> NoReturn: (source)

Dictionary clear() is not implemented for :class:`_sql.ColumnCollection`.

def compare(self, other: ColumnCollection[Any, Any]) -> bool: (source)

Compare this :class:`_expression.ColumnCollection` to another based on the names of the keys

def contains_column(self, col: ColumnElement[Any]) -> bool: (source)

Checks if a column object exists in this collection

def corresponding_column(self, column: _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.

def get(self, key: str, default: Optional[_COL_co] = None) -> Optional[_COL_co]: (source)

Get a :class:`_sql.ColumnClause` or :class:`_schema.Column` object based on a string key name from this :class:`_expression.ColumnCollection`.

def items(self) -> List[Tuple[_COLKEY, _COL_co]]: (source)

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.

def keys(self) -> List[_COLKEY]: (source)

Return a sequence of string key names for all columns in this collection.

def update(self, iter_: Any) -> NoReturn: (source)

Dictionary update() is not implemented for :class:`_sql.ColumnCollection`.

def values(self) -> List[_COL_co]: (source)

Return a sequence of :class:`_sql.ColumnClause` or :class:`_schema.Column` objects for all columns in this collection.

__hash__ = (source)

Undocumented

__slots__: tuple[str, ...] = (source)
def _init_proxy_index(self): (source)

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.

def _initial_populate(self, iter_: Iterable[Tuple[_COLKEY, _COL_co]]): (source)

Undocumented

def _populate_separate_keys(self, iter_: Iterable[Tuple[_COLKEY, _COL_co]]): (source)

populate from an iterator of (key, column)

Undocumented

Undocumented