Produce a :class:`.ColumnClause` object. The :class:`.ColumnClause` is a lightweight analogue to the :class:`_schema.Column` class. The :func:`_expression.column` function can be invoked with just a name alone, as in:: from sqlalchemy import column id, name = colu
(
text: str,
type_: Optional[_TypeEngineArgument[_T]] = None,
is_literal: bool = False,
_selectable: Optional[FromClause] = None,
)
| 968 | |
| 969 | |
| 970 | def column( |
| 971 | text: str, |
| 972 | type_: Optional[_TypeEngineArgument[_T]] = None, |
| 973 | is_literal: bool = False, |
| 974 | _selectable: Optional[FromClause] = None, |
| 975 | ) -> ColumnClause[_T]: |
| 976 | """Produce a :class:`.ColumnClause` object. |
| 977 | |
| 978 | The :class:`.ColumnClause` is a lightweight analogue to the |
| 979 | :class:`_schema.Column` class. The :func:`_expression.column` |
| 980 | function can |
| 981 | be invoked with just a name alone, as in:: |
| 982 | |
| 983 | from sqlalchemy import column |
| 984 | |
| 985 | id, name = column("id"), column("name") |
| 986 | stmt = select(id, name).select_from("user") |
| 987 | |
| 988 | The above statement would produce SQL like: |
| 989 | |
| 990 | .. sourcecode:: sql |
| 991 | |
| 992 | SELECT id, name FROM user |
| 993 | |
| 994 | Once constructed, :func:`_expression.column` |
| 995 | may be used like any other SQL |
| 996 | expression element such as within :func:`_expression.select` |
| 997 | constructs:: |
| 998 | |
| 999 | from sqlalchemy.sql import column |
| 1000 | |
| 1001 | id, name = column("id"), column("name") |
| 1002 | stmt = select(id, name).select_from("user") |
| 1003 | |
| 1004 | The text handled by :func:`_expression.column` |
| 1005 | is assumed to be handled |
| 1006 | like the name of a database column; if the string contains mixed case, |
| 1007 | special characters, or matches a known reserved word on the target |
| 1008 | backend, the column expression will render using the quoting |
| 1009 | behavior determined by the backend. To produce a textual SQL |
| 1010 | expression that is rendered exactly without any quoting, |
| 1011 | use :func:`_expression.literal_column` instead, |
| 1012 | or pass ``True`` as the |
| 1013 | value of :paramref:`_expression.column.is_literal`. Additionally, |
| 1014 | full SQL |
| 1015 | statements are best handled using the :func:`_expression.text` |
| 1016 | construct. |
| 1017 | |
| 1018 | :func:`_expression.column` can be used in a table-like |
| 1019 | fashion by combining it with the :func:`.table` function |
| 1020 | (which is the lightweight analogue to :class:`_schema.Table` |
| 1021 | ) to produce |
| 1022 | a working table construct with minimal boilerplate:: |
| 1023 | |
| 1024 | from sqlalchemy import table, column, select |
| 1025 | |
| 1026 | user = table( |
| 1027 | "user", |