class documentation

class ColumnCollection(object):

Known subclasses: sqlalchemy.sql.base.DedupeColumnCollection, sqlalchemy.sql.base.ImmutableColumnCollection

View In Hierarchy

Collection of _expression.ColumnElement instances, typically for _sql.FromClause objects.

The _sql.ColumnCollection object is most commonly available as the _schema.Table.c or _schema.Table.columns collection on the _schema.Table object, introduced at :ref:`metadata_tables_and_columns`.

The _expression.ColumnCollection has both mapping- and sequence- like behaviors. A _expression.ColumnCollection usually stores _schema.Column objects, which are then accessible both via mapping style access as well as attribute access style.

To access _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 _sql.ColumnCollection object provides a Python dictionary interface, common dictionary method names like _sql.ColumnCollection.keys, _sql.ColumnCollection.values, and _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 _schema.Column would be present is normally that of the :paramref:`_schema.Column.key` parameter. In some contexts, such as a _sql.Select object that uses a label style set using the _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>

.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)
New in version 1.4: _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 _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 _expression.ColumnCollection is used to represent the columns in a SELECT statement which may include duplicates.

A special subclass .DedupeColumnCollection exists which instead maintains SQLAlchemy's older behavior of not allowing duplicates; this collection is used for schema level objects like _schema.Table and .PrimaryKeyConstraint where this deduping is helpful. The .DedupeColumnCollection class also has additional mutation methods as the schema constructs have more use cases that require removal and replacement of columns.

Changed in version 1.4: _expression.ColumnCollection now stores duplicate column keys as well as the same column in multiple positions. The .DedupeColumnCollection class is added to maintain the former behavior in those cases where deduplication as well as additional replace/remove operations are needed.
Method add Add a column to this _sql.ColumnCollection.
Method as​_immutable Return an "immutable" form of this _sql.ColumnCollection.
Method clear Dictionary clear() is not implemented for _sql.ColumnCollection.
Method compare Compare this _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 No summary
Method get Get a _sql.ColumnClause or _schema.Column object based on a string key name from this _expression.ColumnCollection.
Method items No summary
Method keys Return a sequence of string key names for all columns in this collection.
Method remove Dictionary remove() is not implemented for _sql.ColumnCollection.
Method update Dictionary update() is not implemented for _sql.ColumnCollection.
Method values Return a sequence of _sql.ColumnClause or _schema.Column objects for all columns in this collection.
Method __bool__ 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 ​_initial​_populate Undocumented
Method ​_populate​_separate​_keys populate from an iterator of (key, column)
Class Variable __hash__ Undocumented
Class Variable __slots__ Undocumented
Property ​_all​_columns Undocumented
def add(self, column, key=None):

Add a column to this _sql.ColumnCollection.

Note

This method is not normally used by user-facing code, as the _sql.ColumnCollection is usually part of an existing object such as a _schema.Table. To add a _schema.Column to an existing _schema.Table object, use the _schema.Table.append_column method.

def as_immutable(self):
Return an "immutable" form of this _sql.ColumnCollection.
def clear(self):
Dictionary clear() is not implemented for _sql.ColumnCollection.
def compare(self, other):
Compare this _expression.ColumnCollection to another based on the names of the keys
def contains_column(self, col):
Checks if a column object exists in this collection
def corresponding_column(self, column, require_embedded=False):

Given a _expression.ColumnElement, return the exported _expression.ColumnElement object from this _expression.ColumnCollection which corresponds to that original _expression.ColumnElement via a common ancestor column.

See Also

_expression.Selectable.corresponding_column - invokes this method against the collection returned by _expression.Selectable.exported_columns.

Changed in version 1.4: the implementation for corresponding_column was moved onto the _expression.ColumnCollection itself.
Parameters
columnthe target _expression.ColumnElement to be matched.
require​_embeddedonly return corresponding columns for the given _expression.ColumnElement, if the given _expression.ColumnElement is actually present within a sub-element of this _expression.Selectable. Normally the column will match if it merely shares a common ancestor with one of the exported columns of this _expression.Selectable.
def get(self, key, default=None):
Get a _sql.ColumnClause or _schema.Column object based on a string key name from this _expression.ColumnCollection.
def items(self):
Return a sequence of (key, column) tuples for all columns in this collection each consisting of a string key name and a _sql.ColumnClause or _schema.Column object.
def keys(self):
Return a sequence of string key names for all columns in this collection.
def remove(self, column):
Dictionary remove() is not implemented for _sql.ColumnCollection.
def update(self, iter_):
Dictionary update() is not implemented for _sql.ColumnCollection.
def values(self):
Return a sequence of _sql.ColumnClause or _schema.Column objects for all columns in this collection.
def __bool__(self):

Undocumented

def __contains__(self, key):

Undocumented

def __delitem__(self, key):

Undocumented

def __eq__(self, other):

Undocumented

def __getattr__(self, key):

Undocumented

def __getitem__(self, key):

Undocumented

def __getstate__(self):
def __init__(self, columns=None):
def __iter__(self):

Undocumented

def __len__(self):

Undocumented

def __setattr__(self, key, obj):

Undocumented

def __setitem__(self, key, value):

Undocumented

def __setstate__(self, state):
def __str__(self):

Undocumented

def _initial_populate(self, iter_):

Undocumented

def _populate_separate_keys(self, iter_):
populate from an iterator of (key, column)
__hash__ =

Undocumented

__slots__: tuple[str, ...] =
@property
_all_columns =

Undocumented