class ColumnCollection(object):
Known subclasses: sqlalchemy.sql.base.DedupeColumnCollection
, sqlalchemy.sql.base.ImmutableColumnCollection
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)
_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.
_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 |
sqlalchemy.sql.base.DedupeColumnCollection
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.
_expression.ColumnCollection
to another
based on the names of the keysGiven 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
.
_expression.ColumnCollection
itself.Parameters | |
column | the target _expression.ColumnElement
to be matched. |
require_embedded | only 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 . |
_sql.ColumnClause
or _schema.Column
object
based on a string key name from this
_expression.ColumnCollection
._sql.ColumnClause
or
_schema.Column
object.sqlalchemy.sql.base.DedupeColumnCollection
_sql.ColumnCollection
._sql.ColumnClause
or
_schema.Column
objects for all columns in this
collection.sqlalchemy.sql.base.ImmutableColumnCollection
Undocumented
sqlalchemy.sql.base.ImmutableColumnCollection
Undocumented
sqlalchemy.sql.base.DedupeColumnCollection
tuple[ str, ...]
=
sqlalchemy.sql.base.ImmutableColumnCollection
Undocumented