MCPcopy Create free account
hub / github.com/sqlalchemy/sqlalchemy / ColumnCollection

Class ColumnCollection

lib/sqlalchemy/sql/base.py:1510–2021  ·  view source on GitHub ↗

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.Tabl

Source from the content-addressed store, hash-verified

1508
1509
1510class ColumnCollection(Generic[_COLKEY, _COL_co]):
1511 """Collection of :class:`_expression.ColumnElement` instances,
1512 typically for
1513 :class:`_sql.FromClause` objects.
1514
1515 The :class:`_sql.ColumnCollection` object is most commonly available
1516 as the :attr:`_schema.Table.c` or :attr:`_schema.Table.columns` collection
1517 on the :class:`_schema.Table` object, introduced at
1518 :ref:`metadata_tables_and_columns`.
1519
1520 The :class:`_expression.ColumnCollection` has both mapping- and sequence-
1521 like behaviors. A :class:`_expression.ColumnCollection` usually stores
1522 :class:`_schema.Column` objects, which are then accessible both via mapping
1523 style access as well as attribute access style.
1524
1525 To access :class:`_schema.Column` objects using ordinary attribute-style
1526 access, specify the name like any other object attribute, such as below
1527 a column named ``employee_name`` is accessed::
1528
1529 >>> employee_table.c.employee_name
1530
1531 To access columns that have names with special characters or spaces,
1532 index-style access is used, such as below which illustrates a column named
1533 ``employee ' payment`` is accessed::
1534
1535 >>> employee_table.c["employee ' payment"]
1536
1537 As the :class:`_sql.ColumnCollection` object provides a Python dictionary
1538 interface, common dictionary method names like
1539 :meth:`_sql.ColumnCollection.keys`, :meth:`_sql.ColumnCollection.values`,
1540 and :meth:`_sql.ColumnCollection.items` are available, which means that
1541 database columns that are keyed under these names also need to use indexed
1542 access::
1543
1544 >>> employee_table.c["values"]
1545
1546
1547 The name for which a :class:`_schema.Column` would be present is normally
1548 that of the :paramref:`_schema.Column.key` parameter. In some contexts,
1549 such as a :class:`_sql.Select` object that uses a label style set
1550 using the :meth:`_sql.Select.set_label_style` method, a column of a certain
1551 key may instead be represented under a particular label name such
1552 as ``tablename_columnname``::
1553
1554 >>> from sqlalchemy import select, column, table
1555 >>> from sqlalchemy import LABEL_STYLE_TABLENAME_PLUS_COL
1556 >>> t = table("t", column("c"))
1557 >>> stmt = select(t).set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL)
1558 >>> subq = stmt.subquery()
1559 >>> subq.c.t_c
1560 <sqlalchemy.sql.elements.ColumnClause at 0x7f59dcf04fa0; t_c>
1561
1562 :class:`.ColumnCollection` also indexes the columns in order and allows
1563 them to be accessible by their integer position::
1564
1565 >>> cc[0]
1566 Column('x', Integer(), table=None)
1567 >>> cc[1]

Callers 8

_setup_collectionsMethod · 0.85
selected_columnsMethod · 0.85
selected_columnsMethod · 0.85
exported_columnsMethod · 0.85
__getitem__Method · 0.85
cMethod · 0.85
__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected