Selectables, Tables, FROM objects¶
The term “selectable” refers to any object that rows can be selected from;
in SQLAlchemy, these objects descend from FromClause and their
distinguishing feature is their FromClause.c attribute, which is
a namespace of all the columns contained within the FROM clause (these
elements are themselves ColumnElement subclasses).
Selectable Foundational Constructors¶
Top level “FROM clause” and “SELECT” constructors.
| Object Name | Description |
|---|---|
except_(*selects, **kwargs) |
Return an |
except_all(*selects, **kwargs) |
Return an |
exists(*args, **kwargs) |
Construct a new |
intersect(*selects, **kwargs) |
Return an |
intersect_all(*selects, **kwargs) |
Return an |
select(*args, **kw) |
Create a |
table(name, *columns, **kw) |
Produce a new |
union(*selects, **kwargs) |
Return a |
union_all(*selects, **kwargs) |
Return a |
values(*columns, **kw) |
Construct a |
- function sqlalchemy.sql.expression.except_(*selects, **kwargs)¶
Return an
EXCEPTof multiple selectables.The returned object is an instance of
CompoundSelect.
- function sqlalchemy.sql.expression.except_all(*selects, **kwargs)¶
Return an
EXCEPT ALLof multiple selectables.The returned object is an instance of
CompoundSelect.
- function sqlalchemy.sql.expression.exists(*args, **kwargs)¶
Construct a new
Existsconstruct.The
exists()can be invoked by itself to produce anExistsconstruct, which will accept simple WHERE criteria:exists_criteria = exists().where(table1.c.col1 == table2.c.col2)
However, for greater flexibility in constructing the SELECT, an existing
Selectconstruct may be converted to anExists, most conveniently by making use of theSelectBase.exists()method:exists_criteria = ( select(table2.c.col2). where(table1.c.col1 == table2.c.col2). exists() )
The EXISTS criteria is then used inside of an enclosing SELECT:
stmt = select(table1.c.col1).where(exists_criteria)
The above statement will then be of the form:
SELECT col1 FROM table1 WHERE EXISTS (SELECT table2.col2 FROM table2 WHERE table2.col2 = table1.col1)
See also
EXISTS subqueries - in the 2.0 style tutorial.
SelectBase.exists()- method to transform aSELECTto anEXISTSclause.
- function sqlalchemy.sql.expression.intersect(*selects, **kwargs)¶
Return an
INTERSECTof multiple selectables.The returned object is an instance of
CompoundSelect.
- function sqlalchemy.sql.expression.intersect_all(*selects, **kwargs)¶
Return an
INTERSECT ALLof multiple selectables.The returned object is an instance of
CompoundSelect.
- function sqlalchemy.sql.expression.select(*args, **kw)¶
Create a
Selectusing either the 1.x or 2.0 constructor style.For the legacy calling style, see
Select.create_legacy_select(). If the first argument passed is a Python sequence or if keyword arguments are present, this style is used.New in version 2.0: - the
select()construct is the same construct as the one returned byselect(), except that the function only accepts the “columns clause” entities up front; the rest of the state of the SELECT should be built up using generative methods.Similar functionality is also available via the
FromClause.select()method on anyFromClause.- Parameters:
*entities¶ –
Entities to SELECT from. For Core usage, this is typically a series of
ColumnElementand / orFromClauseobjects which will form the columns clause of the resulting statement. For those objects that are instances ofFromClause(typicallyTableorAliasobjects), theFromClause.ccollection is extracted to form a collection ofColumnElementobjects.This parameter will also accept
TextClauseconstructs as given, as well as ORM-mapped classes.
- function sqlalchemy.sql.expression.table(name, *columns, **kw)¶
Produce a new
TableClause.The object returned is an instance of
TableClause, which represents the “syntactical” portion of the schema-levelTableobject. It may be used to construct lightweight table constructs.Changed in version 1.0.0:
table()can now be imported from the plainsqlalchemynamespace like any other SQL element.
- function sqlalchemy.sql.expression.union(*selects, **kwargs)¶
Return a
UNIONof multiple selectables.The returned object is an instance of
CompoundSelect.A similar
union()method is available on allFromClausesubclasses.
- function sqlalchemy.sql.expression.union_all(*selects, **kwargs)¶
Return a
UNION ALLof multiple selectables.The returned object is an instance of
CompoundSelect.A similar
union_all()method is available on allFromClausesubclasses.
- function sqlalchemy.sql.expression.values(*columns, **kw)¶
Construct a
Valuesconstruct.The column expressions and the actual data for
Valuesare given in two separate steps. The constructor receives the column expressions typically ascolumn()constructs, and the data is then passed via theValues.data()method as a list, which can be called multiple times to add more data, e.g.:from sqlalchemy import column from sqlalchemy import values value_expr = values( column('id', Integer), column('name', String), name="my_values" ).data( [(1, 'name1'), (2, 'name2'), (3, 'name3')] )
- Parameters:
*columns¶ – column expressions, typically composed using
column()objects.name¶ – the name for this VALUES construct. If omitted, the VALUES construct will be unnamed in a SQL expression. Different backends may have different requirements here.
literal_binds¶ – Defaults to False. Whether or not to render the data values inline in the SQL output, rather than using bound parameters.
Selectable Modifier Constructors¶
Functions listed here are more commonly available as methods from
FromClause and Selectable elements, for example,
the alias() function is usually invoked via the
FromClause.alias() method.
| Object Name | Description |
|---|---|
alias(selectable[, name, flat]) |
Return an |
cte(selectable[, name, recursive]) |
Return a new |
join(left, right[, onclause, isouter, ...]) |
Produce a |
lateral(selectable[, name]) |
Return a |
outerjoin(left, right[, onclause, full]) |
Return an |
tablesample(selectable, sampling[, name, seed]) |
Return a |
- function sqlalchemy.sql.expression.alias(selectable, name=None, flat=False)¶
Return an
Aliasobject.An
Aliasrepresents anyFromClausewith an alternate name assigned within SQL, typically using theASclause when generated, e.g.SELECT * FROM table AS aliasname.Similar functionality is available via the
FromClause.alias()method available on allFromClausesubclasses. In terms of a SELECT object as generated from theselect()function, theSelectBase.alias()method returns anAliasor similar object which represents a named, parenthesized subquery.When an
Aliasis created from aTableobject, this has the effect of the table being rendered astablename AS aliasnamein a SELECT statement.For
select()objects, the effect is that of creating a named subquery, i.e.(select ...) AS aliasname.The
nameparameter is optional, and provides the name to use in the rendered SQL. If blank, an “anonymous” name will be deterministically generated at compile time. Deterministic means the name is guaranteed to be unique against other constructs used in the same statement, and will also be the same name for each successive compilation of the same statement object.- Parameters:
selectable¶ – any
FromClausesubclass, such as a table, select statement, etc.name¶ – string name to be assigned as the alias. If
None, a name will be deterministically generated at compile time.flat¶ – Will be passed through to if the given selectable is an instance of
Join- seeJoin.alias()for details.
- function sqlalchemy.sql.expression.cte(selectable, name=None, recursive=False)¶
Return a new
CTE, or Common Table Expression instance.Please see
HasCTE.cte()for detail on CTE usage.
- function sqlalchemy.sql.expression.join(left, right, onclause=None, isouter=False, full=False)¶
Produce a
Joinobject, given twoFromClauseexpressions.E.g.:
j = join(user_table, address_table, user_table.c.id == address_table.c.user_id) stmt = select(user_table).select_from(j)
would emit SQL along the lines of:
SELECT user.id, user.name FROM user JOIN address ON user.id = address.user_id
Similar functionality is available given any
FromClauseobject (e.g. such as aTable) using theFromClause.join()method.- Parameters:
left¶ – The left side of the join.
right¶ – the right side of the join; this is any
FromClauseobject such as aTableobject, and may also be a selectable-compatible object such as an ORM-mapped class.onclause¶ – a SQL expression representing the ON clause of the join. If left at
None,FromClause.join()will attempt to join the two tables based on a foreign key relationship.isouter¶ – if True, render a LEFT OUTER JOIN, instead of JOIN.
full¶ –
if True, render a FULL OUTER JOIN, instead of JOIN.
New in version 1.1.
See also
FromClause.join()- method form, based on a given left side.Join- the type of object produced.
- function sqlalchemy.sql.expression.lateral(selectable, name=None)¶
Return a
Lateralobject.Lateralis anAliassubclass that represents a subquery with the LATERAL keyword applied to it.The special behavior of a LATERAL subquery is that it appears in the FROM clause of an enclosing SELECT, but may correlate to other FROM clauses of that SELECT. It is a special case of subquery only supported by a small number of backends, currently more recent PostgreSQL versions.
New in version 1.1.
See also
LATERAL correlation - overview of usage.
- function sqlalchemy.sql.expression.outerjoin(left, right, onclause=None, full=False)¶
Return an
OUTER JOINclause element.The returned object is an instance of
Join.Similar functionality is also available via the
FromClause.outerjoin()method on anyFromClause.- Parameters:
To chain joins together, use the
FromClause.join()orFromClause.outerjoin()methods on the resultingJoinobject.
- function sqlalchemy.sql.expression.tablesample(selectable, sampling, name=None, seed=None)¶
Return a
TableSampleobject.TableSampleis anAliassubclass that represents a table with the TABLESAMPLE clause applied to it.tablesample()is also available from theFromClauseclass via theFromClause.tablesample()method.The TABLESAMPLE clause allows selecting a randomly selected approximate percentage of rows from a table. It supports multiple sampling methods, most commonly BERNOULLI and SYSTEM.
e.g.:
from sqlalchemy import func selectable = people.tablesample( func.bernoulli(1), name='alias', seed=func.random()) stmt = select(selectable.c.people_id)
Assuming
peoplewith a columnpeople_id, the above statement would render as:SELECT alias.people_id FROM people AS alias TABLESAMPLE bernoulli(:bernoulli_1) REPEATABLE (random())
New in version 1.1.
Selectable Class Documentation¶
The classes here are generated using the constructors listed at Selectable Foundational Constructors and Selectable Modifier Constructors.
| Object Name | Description |
|---|---|
Represents an table or selectable alias (AS). |
|
Base class of aliases against tables, subqueries, and other selectables. |
|
Forms the basis of |
|
Represent a Common Table Expression. |
|
Mark a |
|
Represent an |
|
Represent an element that can be used within the |
|
Base class for SELECT statements where additional elements can be added. |
|
Mixin that declares a class to include CTE support. |
|
Represent a |
|
Represent a LATERAL subquery. |
|
The base-most class for Core constructs that have some concept of columns that can represent rows. |
|
Represent a scalar subquery. |
|
Represents a |
|
Mark a class as being selectable. |
|
Base class for SELECT statements. |
|
Represent a subquery of a SELECT. |
|
Represents a minimal “table” construct. |
|
Represent a TABLESAMPLE clause. |
|
An alias against a “table valued” SQL function. |
|
Wrap a |
|
Represent a |
- class sqlalchemy.sql.expression.Alias(*arg, **kw)¶
Represents an table or selectable alias (AS).
Represents an alias, as typically applied to any table or sub-select within a SQL statement using the
ASkeyword (or without the keyword on certain databases such as Oracle).This object is constructed from the
alias()module level function as well as theFromClause.alias()method available on allFromClausesubclasses.See also
Members
Class signature
class
sqlalchemy.sql.expression.Alias(sqlalchemy.sql.roles.DMLTableRole,sqlalchemy.sql.expression.AliasedReturnsRows)-
attribute
sqlalchemy.sql.expression.Alias.inherit_cache = True¶ Indicate if this
HasCacheKeyinstance should make use of the cache key generation scheme used by its immediate superclass.The attribute defaults to
None, which indicates that a construct has not yet taken into account whether or not its appropriate for it to participate in caching; this is functionally equivalent to setting the value toFalse, except that a warning is also emitted.This flag can be set to
Trueon a particular class, if the SQL that corresponds to the object does not change based on attributes which are local to this class, and not its superclass.See also
Enabling Caching Support for Custom Constructs - General guideslines for setting the
HasCacheKey.inherit_cacheattribute for third-party or user defined SQL constructs.
-
attribute
- class sqlalchemy.sql.expression.AliasedReturnsRows(*arg, **kw)¶
Base class of aliases against tables, subqueries, and other selectables.
Members
Class signature
class
sqlalchemy.sql.expression.AliasedReturnsRows(sqlalchemy.sql.expression.NoInit,sqlalchemy.sql.expression.FromClause)-
attribute
sqlalchemy.sql.expression.AliasedReturnsRows.description¶
-
method
sqlalchemy.sql.expression.AliasedReturnsRows.is_derived_from(fromclause)¶ Return
Trueif thisFromClauseis ‘derived’ from the givenFromClause.An example would be an Alias of a Table is derived from that Table.
-
attribute
sqlalchemy.sql.expression.AliasedReturnsRows.original¶ Legacy for dialects that are referring to Alias.original.
-
attribute
- class sqlalchemy.sql.expression.CompoundSelect(keyword, *selects, **kwargs)¶
Forms the basis of
UNION,UNION ALL, and other SELECT-based set operations.Members
add_cte(), alias(), append_group_by(), append_order_by(), apply_labels(), as_scalar(), bind, c, corresponding_column(), cte(), execute(), execution_options(), exists(), exported_columns, fetch(), get_execution_options(), get_label_style(), group_by(), label(), lateral(), limit(), offset(), options(), order_by(), replace_selectable(), scalar(), scalar_subquery(), select(), selected_columns, self_group(), set_label_style(), slice(), subquery(), with_for_update()
Class signature
class
sqlalchemy.sql.expression.CompoundSelect(sqlalchemy.sql.expression.HasCompileState,sqlalchemy.sql.expression.GenerativeSelect)-
method
sqlalchemy.sql.expression.CompoundSelect.add_cte(cte)¶ inherited from the
HasCTE.add_cte()method ofHasCTEAdd a
CTEto this statement object that will be independently rendered even if not referenced in the statement otherwise.This feature is useful for the use case of embedding a DML statement such as an INSERT or UPDATE as a CTE inline with a primary statement that may draw from its results indirectly; while PostgreSQL is known to support this usage, it may not be supported by other backends.
E.g.:
from sqlalchemy import table, column, select t = table('t', column('c1'), column('c2')) ins = t.insert().values({"c1": "x", "c2": "y"}).cte() stmt = select(t).add_cte(ins)
Would render:
WITH anon_1 AS (INSERT INTO t (c1, c2) VALUES (:param_1, :param_2)) SELECT t.c1, t.c2 FROM t
Above, the “anon_1” CTE is not referred towards in the SELECT statement, however still accomplishes the task of running an INSERT statement.
Similarly in a DML-related context, using the PostgreSQL
Insertconstruct to generate an “upsert”:from sqlalchemy import table, column from sqlalchemy.dialects.postgresql import insert t = table("t", column("c1"), column("c2")) delete_statement_cte = ( t.delete().where(t.c.c1 < 1).cte("deletions") ) insert_stmt = insert(t).values({"c1": 1, "c2": 2}) update_statement = insert_stmt.on_conflict_do_update( index_elements=[t.c.c1], set_={ "c1": insert_stmt.excluded.c1, "c2": insert_stmt.excluded.c2, }, ).add_cte(delete_statement_cte) print(update_statement)
The above statement renders as:
WITH deletions AS (DELETE FROM t WHERE t.c1 < %(c1_1)s) INSERT INTO t (c1, c2) VALUES (%(c1)s, %(c2)s) ON CONFLICT (c1) DO UPDATE SET c1 = excluded.c1, c2 = excluded.c2
New in version 1.4.21.
-
method
sqlalchemy.sql.expression.CompoundSelect.alias(name=None, flat=False)¶ inherited from the
SelectBase.alias()method ofSelectBaseReturn a named subquery against this
SelectBase.For a
SelectBase(as opposed to aFromClause), this returns aSubqueryobject which behaves mostly the same as theAliasobject that is used with aFromClause.Changed in version 1.4: The
SelectBase.alias()method is now a synonym for theSelectBase.subquery()method.
-
method
sqlalchemy.sql.expression.CompoundSelect.append_group_by(*clauses)¶ inherited from the
DeprecatedSelectBaseGenerations.append_group_by()method ofDeprecatedSelectBaseGenerationsAppend the given GROUP BY criterion applied to this selectable.
Deprecated since version 1.4: The
GenerativeSelect.append_group_by()method is deprecated and will be removed in a future release. Use the generative methodGenerativeSelect.group_by().The criterion will be appended to any pre-existing GROUP BY criterion.
This is an in-place mutation method; the
GenerativeSelect.group_by()method is preferred, as it provides standard method chaining.
-
method
sqlalchemy.sql.expression.CompoundSelect.append_order_by(*clauses)¶ inherited from the
DeprecatedSelectBaseGenerations.append_order_by()method ofDeprecatedSelectBaseGenerationsAppend the given ORDER BY criterion applied to this selectable.
Deprecated since version 1.4: The
GenerativeSelect.append_order_by()method is deprecated and will be removed in a future release. Use the generative methodGenerativeSelect.order_by().The criterion will be appended to any pre-existing ORDER BY criterion.
This is an in-place mutation method; the
GenerativeSelect.order_by()method is preferred, as it provides standard method chaining.See also
-
method
sqlalchemy.sql.expression.CompoundSelect.apply_labels()¶ inherited from the
GenerativeSelect.apply_labels()method ofGenerativeSelectDeprecated since version 1.4: The
GenerativeSelect.apply_labels()method is considered legacy as of the 1.x series of SQLAlchemy and will be removed in 2.0. Use set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) instead. (Background on SQLAlchemy 2.0 at: Migrating to SQLAlchemy 2.0)
-
method
sqlalchemy.sql.expression.CompoundSelect.as_scalar()¶ inherited from the
SelectBase.as_scalar()method ofSelectBaseDeprecated since version 1.4: The
SelectBase.as_scalar()method is deprecated and will be removed in a future release. Please refer toSelectBase.scalar_subquery().
-
attribute
sqlalchemy.sql.expression.CompoundSelect.bind¶ Returns the
EngineorConnectionto which thisExecutableis bound, or None if none found.Deprecated since version 1.4: The
Executable.bindattribute is considered legacy as of the 1.x series of SQLAlchemy and will be removed in 2.0. Bound metadata is being removed as of SQLAlchemy 2.0. (Background on SQLAlchemy 2.0 at: Migrating to SQLAlchemy 2.0)
-
attribute
sqlalchemy.sql.expression.CompoundSelect.c¶ inherited from the
SelectBase.cattribute ofSelectBaseDeprecated since version 1.4: The
SelectBase.candSelectBase.columnsattributes are deprecated and will be removed in a future release; these attributes implicitly create a subquery that should be explicit. Please callSelectBase.subquery()first in order to create a subquery, which then contains this attribute. To access the columns that this SELECT object SELECTs from, use theSelectBase.selected_columnsattribute.
-
method
sqlalchemy.sql.expression.CompoundSelect.corresponding_column(column, require_embedded=False)¶ inherited from the
Selectable.corresponding_column()method ofSelectableGiven a
ColumnElement, return the exportedColumnElementobject from theSelectable.exported_columnscollection of thisSelectablewhich corresponds to that originalColumnElementvia a common ancestor column.- Parameters:
column¶ – the target
ColumnElementto be matched.require_embedded¶ – only return corresponding columns for the given
ColumnElement, if the givenColumnElementis actually present within a sub-element of thisSelectable. Normally the column will match if it merely shares a common ancestor with one of the exported columns of thisSelectable.
See also
Selectable.exported_columns- theColumnCollectionthat is used for the operation.ColumnCollection.corresponding_column()- implementation method.
-
method
sqlalchemy.sql.expression.CompoundSelect.cte(name=None, recursive=False, nesting=False)¶ inherited from the
HasCTE.cte()method ofHasCTEReturn a new
CTE, or Common Table Expression instance.Common table expressions are a SQL standard whereby SELECT statements can draw upon secondary statements specified along with the primary statement, using a clause called “WITH”. Special semantics regarding UNION can also be employed to allow “recursive” queries, where a SELECT statement can draw upon the set of rows that have previously been selected.
CTEs can also be applied to DML constructs UPDATE, INSERT and DELETE on some databases, both as a source of CTE rows when combined with RETURNING, as well as a consumer of CTE rows.
Changed in version 1.1: Added support for UPDATE/INSERT/DELETE as CTE, CTEs added to UPDATE/INSERT/DELETE.
SQLAlchemy detects
CTEobjects, which are treated similarly toAliasobjects, as special elements to be delivered to the FROM clause of the statement as well as to a WITH clause at the top of the statement.For special prefixes such as PostgreSQL “MATERIALIZED” and “NOT MATERIALIZED”, the
CTE.prefix_with()method may be used to establish these.Changed in version 1.3.13: Added support for prefixes. In particular - MATERIALIZED and NOT MATERIALIZED.
- Parameters:
name¶ – name given to the common table expression. Like
FromClause.alias(), the name can be left asNonein which case an anonymous symbol will be used at query compile time.recursive¶ – if
True, will renderWITH RECURSIVE. A recursive common table expression is intended to be used in conjunction with UNION ALL in order to derive rows from those already selected.nesting¶ –
if
True, will render the CTE locally to the actual statement.New in version 1.4.24.
The following examples include two from PostgreSQL’s documentation at https://www.postgresql.org/docs/current/static/queries-with.html, as well as additional examples.
Example 1, non recursive:
from sqlalchemy import (Table, Column, String, Integer, MetaData, select, func) metadata = MetaData() orders = Table('orders', metadata, Column('region', String), Column('amount', Integer), Column('product', String), Column('quantity', Integer) ) regional_sales = select( orders.c.region, func.sum(orders.c.amount).label('total_sales') ).group_by(orders.c.region).cte("regional_sales") top_regions = select(regional_sales.c.region).\ where( regional_sales.c.total_sales > select( func.sum(regional_sales.c.total_sales) / 10 ) ).cte("top_regions") statement = select( orders.c.region, orders.c.product, func.sum(orders.c.quantity).label("product_units"), func.sum(orders.c.amount).label("product_sales") ).where(orders.c.region.in_( select(top_regions.c.region) )).group_by(orders.c.region, orders.c.product) result = conn.execute(statement).fetchall()
Example 2, WITH RECURSIVE:
from sqlalchemy import (Table, Column, String, Integer, MetaData, select, func) metadata = MetaData() parts = Table('parts', metadata, Column('part', String), Column('sub_part', String), Column('quantity', Integer), ) included_parts = select(\ parts.c.sub_part, parts.c.part, parts.c.quantity\ ).\ where(parts.c.part=='our part').\ cte(recursive=True) incl_alias = included_parts.alias() parts_alias = parts.alias() included_parts = included_parts.union_all( select( parts_alias.c.sub_part, parts_alias.c.part, parts_alias.c.quantity ).\ where(parts_alias.c.part==incl_alias.c.sub_part) ) statement = select( included_parts.c.sub_part, func.sum(included_parts.c.quantity). label('total_quantity') ).\ group_by(included_parts.c.sub_part) result = conn.execute(statement).fetchall()
Example 3, an upsert using UPDATE and INSERT with CTEs:
from datetime import date from sqlalchemy import (MetaData, Table, Column, Integer, Date, select, literal, and_, exists) metadata = MetaData() visitors = Table('visitors', metadata, Column('product_id', Integer, primary_key=True), Column('date', Date, primary_key=True), Column('count', Integer), ) # add 5 visitors for the product_id == 1 product_id = 1 day = date.today() count = 5 update_cte = ( visitors.update() .where(and_(visitors.c.product_id == product_id, visitors.c.date == day)) .values(count=visitors.c.count + count) .returning(literal(1)) .cte('update_cte') ) upsert = visitors.insert().from_select( [visitors.c.product_id, visitors.c.date, visitors.c.count], select(literal(product_id), literal(day), literal(count)) .where(~exists(update_cte.select())) ) connection.execute(upsert)
Example 4, Nesting CTE (SQLAlchemy 1.4.24 and above):
value_a = select( literal("root").label("n") ).cte("value_a") # A nested CTE with the same name as the root one value_a_nested = select( literal("nesting").label("n") ).cte("value_a", nesting=True) # Nesting CTEs takes ascendency locally # over the CTEs at a higher level value_b = select(value_a_nested.c.n).cte("value_b") value_ab = select(value_a.c.n.label("a"), value_b.c.n.label("b"))
The above query will render the second CTE nested inside the first, shown with inline parameters below as:
WITH value_a AS (SELECT 'root' AS n), value_b AS (WITH value_a AS (SELECT 'nesting' AS n) SELECT value_a.n AS n FROM value_a) SELECT value_a.n AS a, value_b.n AS b FROM value_a, value_b
Example 5, Non-Linear CTE (SQLAlchemy 1.4.28 and above):
edge = Table( "edge", metadata, Column("id", Integer, primary_key=True), Column("left", Integer), Column("right", Integer), ) root_node = select(literal(1).label("node")).cte( "nodes", recursive=True ) left_edge = select(edge.c.left).join( root_node, edge.c.right == root_node.c.node ) right_edge = select(edge.c.right).join( root_node, edge.c.left == root_node.c.node ) subgraph_cte = root_node.union(left_edge, right_edge) subgraph = select(subgraph_cte)
The above query will render 2 UNIONs inside the recursive CTE:
WITH RECURSIVE nodes(node) AS ( SELECT 1 AS node UNION SELECT edge."left" AS "left" FROM edge JOIN nodes ON edge."right" = nodes.node UNION SELECT edge."right" AS "right" FROM edge JOIN nodes ON edge."left" = nodes.node ) SELECT nodes.node FROM nodes
See also
Query.cte()- ORM version ofHasCTE.cte().
-
method
sqlalchemy.sql.expression.CompoundSelect.execute(*multiparams, **params)¶ inherited from the
Executable.execute()method ofExecutableCompile and execute this
Executable.Deprecated since version 1.4: The
Executable.execute()method is considered legacy as of the 1.x series of SQLAlchemy and will be removed in 2.0. All statement execution in SQLAlchemy 2.0 is performed by theConnection.execute()method ofConnection, or in the ORM by theSession.execute()method ofSession. (Background on SQLAlchemy 2.0 at: Migrating to SQLAlchemy 2.0)
-
method
sqlalchemy.sql.expression.CompoundSelect.execution_options(**kw)¶ inherited from the
Executable.execution_options()method ofExecutableSet non-SQL options for the statement which take effect during execution.
Execution options can be set on a per-statement or per
Connectionbasis. Additionally, theEngineand ORMQueryobjects provide access to execution options which they in turn configure upon connections.The
execution_options()method is generative. A new instance of this statement is returned that contains the options:statement = select(table.c.x, table.c.y) statement = statement.execution_options(autocommit=True)
Note that only a subset of possible execution options can be applied to a statement - these include “autocommit” and “stream_results”, but not “isolation_level” or “compiled_cache”. See
Connection.execution_options()for a full list of possible options.
-
method
sqlalchemy.sql.expression.CompoundSelect.exists()¶ inherited from the
SelectBase.exists()method ofSelectBaseReturn an
Existsrepresentation of this selectable, which can be used as a column expression.The returned object is an instance of
Exists.New in version 1.4.
-
attribute
sqlalchemy.sql.expression.CompoundSelect.exported_columns¶ inherited from the
SelectBase.exported_columnsattribute ofSelectBaseA
ColumnCollectionthat represents the “exported” columns of thisSelectable, not includingTextClauseconstructs.The “exported” columns for a
SelectBaseobject are synonymous with theSelectBase.selected_columnscollection.New in version 1.4.
-
method
sqlalchemy.sql.expression.CompoundSelect.fetch(count, with_ties=False, percent=False)¶ inherited from the
GenerativeSelect.fetch()method ofGenerativeSelectReturn a new selectable with the given FETCH FIRST criterion applied.
This is a numeric value which usually renders as
FETCH {FIRST | NEXT} [ count ] {ROW | ROWS} {ONLY | WITH TIES}expression in the resulting select. This functionality is is currently implemented for Oracle, PostgreSQL, MSSQL.Use
GenerativeSelect.offset()to specify the offset.Note
The
GenerativeSelect.fetch()method will replace any clause applied withGenerativeSelect.limit().New in version 1.4.
- Parameters:
count¶ – an integer COUNT parameter, or a SQL expression that provides an integer result. When
percent=Truethis will represent the percentage of rows to return, not the absolute value. PassNoneto reset it.with_ties¶ – When
True, the WITH TIES option is used to return any additional rows that tie for the last place in the result set according to theORDER BYclause. TheORDER BYmay be mandatory in this case. Defaults toFalsepercent¶ – When
True,countrepresents the percentage of the total number of selected rows to return. Defaults toFalse
-
method
sqlalchemy.sql.expression.CompoundSelect.get_execution_options()¶ inherited from the
Executable.get_execution_options()method ofExecutableGet the non-SQL options which will take effect during execution.
New in version 1.3.
See also
-
method
sqlalchemy.sql.expression.CompoundSelect.get_label_style()¶ inherited from the
GenerativeSelect.get_label_style()method ofGenerativeSelectRetrieve the current label style.
New in version 1.4.
-
method
sqlalchemy.sql.expression.CompoundSelect.group_by(*clauses)¶ inherited from the
GenerativeSelect.group_by()method ofGenerativeSelectReturn a new selectable with the given list of GROUP BY criterion applied.
All existing GROUP BY settings can be suppressed by passing
None.e.g.:
stmt = select(table.c.name, func.max(table.c.stat)).\ group_by(table.c.name)
- Parameters:
*clauses¶ – a series of
ColumnElementconstructs which will be used to generate an GROUP BY clause.
-
method
sqlalchemy.sql.expression.CompoundSelect.label(name)¶ inherited from the
SelectBase.label()method ofSelectBaseReturn a ‘scalar’ representation of this selectable, embedded as a subquery with a label.
See also
-
method
sqlalchemy.sql.expression.CompoundSelect.lateral(name=None)¶ inherited from the
SelectBase.lateral()method ofSelectBaseReturn a LATERAL alias of this
Selectable.The return value is the
Lateralconstruct also provided by the top-levellateral()function.New in version 1.1.
See also
LATERAL correlation - overview of usage.
-
method
sqlalchemy.sql.expression.CompoundSelect.limit(limit)¶ inherited from the
GenerativeSelect.limit()method ofGenerativeSelectReturn a new selectable with the given LIMIT criterion applied.
This is a numerical value which usually renders as a
LIMITexpression in the resulting select. Backends that don’t supportLIMITwill attempt to provide similar functionality.Note
The
GenerativeSelect.limit()method will replace any clause applied withGenerativeSelect.fetch().Changed in version 1.0.0: -
Select.limit()can now accept arbitrary SQL expressions as well as integer values.- Parameters:
limit¶ – an integer LIMIT parameter, or a SQL expression that provides an integer result. Pass
Noneto reset it.
-
method
sqlalchemy.sql.expression.CompoundSelect.offset(offset)¶ inherited from the
GenerativeSelect.offset()method ofGenerativeSelectReturn a new selectable with the given OFFSET criterion applied.
This is a numeric value which usually renders as an
OFFSETexpression in the resulting select. Backends that don’t supportOFFSETwill attempt to provide similar functionality.Changed in version 1.0.0: -
Select.offset()can now accept arbitrary SQL expressions as well as integer values.- Parameters:
offset¶ – an integer OFFSET parameter, or a SQL expression that provides an integer result. Pass
Noneto reset it.
-
method
sqlalchemy.sql.expression.CompoundSelect.options(*options)¶ inherited from the
Executable.options()method ofExecutableApply options to this statement.
In the general sense, options are any kind of Python object that can be interpreted by the SQL compiler for the statement. These options can be consumed by specific dialects or specific kinds of compilers.
The most commonly known kind of option are the ORM level options that apply “eager load” and other loading behaviors to an ORM query. However, options can theoretically be used for many other purposes.
For background on specific kinds of options for specific kinds of statements, refer to the documentation for those option objects.
Changed in version 1.4: - added
Generative.options()to Core statement objects towards the goal of allowing unified Core / ORM querying capabilities.See also
Deferred Column Loader Query Options - refers to options specific to the usage of ORM queries
Relationship Loading with Loader Options - refers to options specific to the usage of ORM queries
-
method
sqlalchemy.sql.expression.CompoundSelect.order_by(*clauses)¶ inherited from the
GenerativeSelect.order_by()method ofGenerativeSelectReturn a new selectable with the given list of ORDER BY criteria applied.
e.g.:
stmt = select(table).order_by(table.c.id, table.c.name)
Calling this method multiple times is equivalent to calling it once with all the clauses concatenated. All existing ORDER BY criteria may be cancelled by passing
Noneby itself. New ORDER BY criteria may then be added by invokingQuery.order_by()again, e.g.:# will erase all ORDER BY and ORDER BY new_col alone stmt = stmt.order_by(None).order_by(new_col)
- Parameters:
*clauses¶ – a series of
ColumnElementconstructs which will be used to generate an ORDER BY clause.
See also
ORDER BY - in the SQLAlchemy 1.4 / 2.0 Tutorial
Ordering or Grouping by a Label - in the SQLAlchemy 1.4 / 2.0 Tutorial
-
method
sqlalchemy.sql.expression.CompoundSelect.replace_selectable(old, alias)¶ inherited from the
Selectable.replace_selectable()method ofSelectableReplace all occurrences of
FromClause‘old’ with the givenAliasobject, returning a copy of thisFromClause.Deprecated since version 1.4: The
Selectable.replace_selectable()method is deprecated, and will be removed in a future release. Similar functionality is available via the sqlalchemy.sql.visitors module.
-
method
sqlalchemy.sql.expression.CompoundSelect.scalar(*multiparams, **params)¶ inherited from the
Executable.scalar()method ofExecutableCompile and execute this
Executable, returning the result’s scalar representation.Deprecated since version 1.4: The
Executable.scalar()method is considered legacy as of the 1.x series of SQLAlchemy and will be removed in 2.0. Scalar execution in SQLAlchemy 2.0 is performed by theConnection.scalar()method ofConnection, or in the ORM by theSession.scalar()method ofSession. (Background on SQLAlchemy 2.0 at: Migrating to SQLAlchemy 2.0)
-
method
sqlalchemy.sql.expression.CompoundSelect.scalar_subquery()¶ inherited from the
SelectBase.scalar_subquery()method ofSelectBaseReturn a ‘scalar’ representation of this selectable, which can be used as a column expression.
The returned object is an instance of
ScalarSelect.Typically, a select statement which has only one column in its columns clause is eligible to be used as a scalar expression. The scalar subquery can then be used in the WHERE clause or columns clause of an enclosing SELECT.
Note that the scalar subquery differentiates from the FROM-level subquery that can be produced using the
SelectBase.subquery()method.See also
Scalar and Correlated Subqueries - in the 2.0 tutorial
-
method
sqlalchemy.sql.expression.CompoundSelect.select(*arg, **kw)¶ inherited from the
SelectBase.select()method ofSelectBaseDeprecated since version 1.4: The
SelectBase.select()method is deprecated and will be removed in a future release; this method implicitly creates a subquery that should be explicit. Please callSelectBase.subquery()first in order to create a subquery, which then can be selected.
-
attribute
sqlalchemy.sql.expression.CompoundSelect.selected_columns¶ A
ColumnCollectionrepresenting the columns that this SELECT statement or similar construct returns in its result set, not includingTextClauseconstructs.For a
CompoundSelect, theCompoundSelect.selected_columnsattribute returns the selected columns of the first SELECT statement contained within the series of statements within the set operation.See also
New in version 1.4.
-
method
sqlalchemy.sql.expression.CompoundSelect.self_group(against=None)¶ Apply a ‘grouping’ to this
ClauseElement.This method is overridden by subclasses to return a “grouping” construct, i.e. parenthesis. In particular it’s used by “binary” expressions to provide a grouping around themselves when placed into a larger expression, as well as by
select()constructs when placed into the FROM clause of anotherselect(). (Note that subqueries should be normally created using theSelect.alias()method, as many platforms require nested SELECT statements to be named).As expressions are composed together, the application of
self_group()is automatic - end-user code should never need to use this method directly. Note that SQLAlchemy’s clause constructs take operator precedence into account - so parenthesis might not be needed, for example, in an expression likex OR (y AND z)- AND takes precedence over OR.The base
self_group()method ofClauseElementjust returns self.
-
method
sqlalchemy.sql.expression.CompoundSelect.set_label_style(style)¶ inherited from the
GenerativeSelect.set_label_style()method ofGenerativeSelectReturn a new selectable with the specified label style.
There are three “label styles” available,
LABEL_STYLE_DISAMBIGUATE_ONLY,LABEL_STYLE_TABLENAME_PLUS_COL, andLABEL_STYLE_NONE. The default style isLABEL_STYLE_DISAMBIGUATE_ONLY.In modern SQLAlchemy, there is not generally a need to change the labeling style, as per-expression labels are more effectively used by making use of the
ColumnElement.label()method. In past versions,LABEL_STYLE_TABLENAME_PLUS_COLwas used to disambiguate same-named columns from different tables, aliases, or subqueries; the newerLABEL_STYLE_DISAMBIGUATE_ONLYnow applies labels only to names that conflict with an existing name so that the impact of this labeling is minimal.The rationale for disambiguation is mostly so that all column expressions are available from a given
FromClause.ccollection when a subquery is created.New in version 1.4: - the
GenerativeSelect.set_label_style()method replaces the previous combination of.apply_labels(),.with_labels()anduse_labels=Truemethods and/or parameters.
-
method
sqlalchemy.sql.expression.CompoundSelect.slice(start, stop)¶ inherited from the
GenerativeSelect.slice()method ofGenerativeSelectApply LIMIT / OFFSET to this statement based on a slice.
The start and stop indices behave like the argument to Python’s built-in
range()function. This method provides an alternative to usingLIMIT/OFFSETto get a slice of the query.For example,
stmt = select(User).order_by(User).id.slice(1, 3)
renders as
SELECT users.id AS users_id, users.name AS users_name FROM users ORDER BY users.id LIMIT ? OFFSET ? (2, 1)
Note
The
GenerativeSelect.slice()method will replace any clause applied withGenerativeSelect.fetch().New in version 1.4: Added the
GenerativeSelect.slice()method generalized from the ORM.
-
method
sqlalchemy.sql.expression.CompoundSelect.subquery(name=None)¶ inherited from the
SelectBase.subquery()method ofSelectBaseReturn a subquery of this
SelectBase.A subquery is from a SQL perspective a parenthesized, named construct that can be placed in the FROM clause of another SELECT statement.
Given a SELECT statement such as:
stmt = select(table.c.id, table.c.name)
The above statement might look like:
SELECT table.id, table.name FROM table
The subquery form by itself renders the same way, however when embedded into the FROM clause of another SELECT statement, it becomes a named sub-element:
subq = stmt.subquery() new_stmt = select(subq)
The above renders as:
SELECT anon_1.id, anon_1.name FROM (SELECT table.id, table.name FROM table) AS anon_1
Historically,
SelectBase.subquery()is equivalent to calling theFromClause.alias()method on a FROM object; however, as aSelectBaseobject is not directly FROM object, theSelectBase.subquery()method provides clearer semantics.New in version 1.4.
-
method
sqlalchemy.sql.expression.CompoundSelect.with_for_update(nowait=False, read=False, of=None, skip_locked=False, key_share=False)¶ inherited from the
GenerativeSelect.with_for_update()method ofGenerativeSelectSpecify a
FOR UPDATEclause for thisGenerativeSelect.E.g.:
stmt = select(table).with_for_update(nowait=True)
On a database like PostgreSQL or Oracle, the above would render a statement like:
SELECT table.a, table.b FROM table FOR UPDATE NOWAIT
on other backends, the
nowaitoption is ignored and instead would produce:SELECT table.a, table.b FROM table FOR UPDATE
When called with no arguments, the statement will render with the suffix
FOR UPDATE. Additional arguments can then be provided which allow for common database-specific variants.- Parameters:
nowait¶ – boolean; will render
FOR UPDATE NOWAITon Oracle and PostgreSQL dialects.read¶ – boolean; will render
LOCK IN SHARE MODEon MySQL,FOR SHAREon PostgreSQL. On PostgreSQL, when combined withnowait, will renderFOR SHARE NOWAIT.of¶ – SQL expression or list of SQL expression elements (typically
Columnobjects or a compatible expression) which will render into aFOR UPDATE OFclause; supported by PostgreSQL and Oracle. May render as a table or as a column depending on backend.skip_locked¶ – boolean, will render
FOR UPDATE SKIP LOCKEDon Oracle and PostgreSQL dialects orFOR SHARE SKIP LOCKEDifread=Trueis also specified.key_share¶ – boolean, will render
FOR NO KEY UPDATE, or if combined withread=Truewill renderFOR KEY SHARE, on the PostgreSQL dialect.
-
method
- class sqlalchemy.sql.expression.CTE(*arg, **kw)¶
Represent a Common Table Expression.
The
CTEobject is obtained using theSelectBase.cte()method from any SELECT statement. A less often available syntax also allows use of theHasCTE.cte()method present on DML constructs such asInsert,UpdateandDelete. See theHasCTE.cte()method for usage details on CTEs.Members
Class signature
class
sqlalchemy.sql.expression.CTE(sqlalchemy.sql.roles.DMLTableRole,sqlalchemy.sql.roles.IsCTERole,sqlalchemy.sql.expression.Generative,sqlalchemy.sql.expression.HasPrefixes,sqlalchemy.sql.expression.HasSuffixes,sqlalchemy.sql.expression.AliasedReturnsRows)-
method
sqlalchemy.sql.expression.CTE.alias(name=None, flat=False)¶ -
This method is a CTE-specific specialization of the
FromClause.alias()method.
-
method
sqlalchemy.sql.expression.CTE.union(*other)¶ Return a new
CTEwith a SQLUNIONof the original CTE against the given selectables provided as positional arguments.- Parameters:
*other¶ –
one or more elements with which to create a UNION.
Changed in version 1.4.28: multiple elements are now accepted.
See also
HasCTE.cte()- examples of calling styles
-
method
sqlalchemy.sql.expression.CTE.union_all(*other)¶ Return a new
CTEwith a SQLUNION ALLof the original CTE against the given selectables provided as positional arguments.- Parameters:
*other¶ –
one or more elements with which to create a UNION.
Changed in version 1.4.28: multiple elements are now accepted.
See also
HasCTE.cte()- examples of calling styles
-
method
- class sqlalchemy.sql.expression.Executable¶
Mark a
ClauseElementas supporting execution.Executableis a superclass for all “statement” types of objects, includingselect(),delete(),update(),insert(),text().Members
bind, execute(), execution_options(), get_execution_options(), options(), scalar()
Class signature
class
sqlalchemy.sql.expression.Executable(sqlalchemy.sql.roles.StatementRole,sqlalchemy.sql.expression.Generative)-
attribute
sqlalchemy.sql.expression.Executable.bind¶ Returns the
EngineorConnectionto which thisExecutableis bound, or None if none found.Deprecated since version 1.4: The
Executable.bindattribute is considered legacy as of the 1.x series of SQLAlchemy and will be removed in 2.0. Bound metadata is being removed as of SQLAlchemy 2.0. (Background on SQLAlchemy 2.0 at: Migrating to SQLAlchemy 2.0)This is a traversal which checks locally, then checks among the “from” clauses of associated objects until a bound engine or connection is found.
-
method
sqlalchemy.sql.expression.Executable.execute(*multiparams, **params)¶ Compile and execute this
Executable.Deprecated since version 1.4: The
Executable.execute()method is considered legacy as of the 1.x series of SQLAlchemy and will be removed in 2.0. All statement execution in SQLAlchemy 2.0 is performed by theConnection.execute()method ofConnection, or in the ORM by theSession.execute()method ofSession. (Background on SQLAlchemy 2.0 at: Migrating to SQLAlchemy 2.0)
-
method
sqlalchemy.sql.expression.Executable.execution_options(**kw)¶ Set non-SQL options for the statement which take effect during execution.
Execution options can be set on a per-statement or per
Connectionbasis. Additionally, theEngineand ORMQueryobjects provide access to execution options which they in turn configure upon connections.The
execution_options()method is generative. A new instance of this statement is returned that contains the options:statement = select(table.c.x, table.c.y) statement = statement.execution_options(autocommit=True)
Note that only a subset of possible execution options can be applied to a statement - these include “autocommit” and “stream_results”, but not “isolation_level” or “compiled_cache”. See
Connection.execution_options()for a full list of possible options.
-
method
sqlalchemy.sql.expression.Executable.get_execution_options()¶ Get the non-SQL options which will take effect during execution.
New in version 1.3.
See also
-
method
sqlalchemy.sql.expression.Executable.options(*options)¶ Apply options to this statement.
In the general sense, options are any kind of Python object that can be interpreted by the SQL compiler for the statement. These options can be consumed by specific dialects or specific kinds of compilers.
The most commonly known kind of option are the ORM level options that apply “eager load” and other loading behaviors to an ORM query. However, options can theoretically be used for many other purposes.
For background on specific kinds of options for specific kinds of statements, refer to the documentation for those option objects.
Changed in version 1.4: - added
Generative.options()to Core statement objects towards the goal of allowing unified Core / ORM querying capabilities.See also
Deferred Column Loader Query Options - refers to options specific to the usage of ORM queries
Relationship Loading with Loader Options - refers to options specific to the usage of ORM queries
-
method
sqlalchemy.sql.expression.Executable.scalar(*multiparams, **params)¶ Compile and execute this
Executable, returning the result’s scalar representation.Deprecated since version 1.4: The
Executable.scalar()method is considered legacy as of the 1.x series of SQLAlchemy and will be removed in 2.0. Scalar execution in SQLAlchemy 2.0 is performed by theConnection.scalar()method ofConnection, or in the ORM by theSession.scalar()method ofSession. (Background on SQLAlchemy 2.0 at: Migrating to SQLAlchemy 2.0)
-
attribute
- class sqlalchemy.sql.expression.Exists(*args, **kwargs)¶
Represent an
EXISTSclause.See
exists()for a description of usage.An
EXISTSclause can also be constructed from aselect()instance by callingSelectBase.exists().Members
__init__(), correlate(), correlate_except(), inherit_cache, select(), select_from(), where()
Class signature
class
sqlalchemy.sql.expression.Exists(sqlalchemy.sql.expression.UnaryExpression)-
method
sqlalchemy.sql.expression.Exists.__init__(*args, **kwargs)¶ Construct a new
Existsobject.This constructor is mirrored as a public API function; see
sqlalchemy.sql.expression.exists()for a full usage and argument description.
-
method
sqlalchemy.sql.expression.Exists.correlate(*fromclause)¶ Apply correlation to the subquery noted by this
Exists.See also
-
method
sqlalchemy.sql.expression.Exists.correlate_except(*fromclause)¶ Apply correlation to the subquery noted by this
Exists.See also
-
attribute
sqlalchemy.sql.expression.Exists.inherit_cache = True¶ Indicate if this
HasCacheKeyinstance should make use of the cache key generation scheme used by its immediate superclass.The attribute defaults to
None, which indicates that a construct has not yet taken into account whether or not its appropriate for it to participate in caching; this is functionally equivalent to setting the value toFalse, except that a warning is also emitted.This flag can be set to
Trueon a particular class, if the SQL that corresponds to the object does not change based on attributes which are local to this class, and not its superclass.See also
Enabling Caching Support for Custom Constructs - General guideslines for setting the
HasCacheKey.inherit_cacheattribute for third-party or user defined SQL constructs.
-
method
sqlalchemy.sql.expression.Exists.select(whereclause=None, **kwargs)¶ Return a SELECT of this
Exists.e.g.:
stmt = exists(some_table.c.id).where(some_table.c.id == 5).select()
This will produce a statement resembling:
SELECT EXISTS (SELECT id FROM some_table WHERE some_table = :param) AS anon_1
- Parameters:
whereclause¶ –
a WHERE clause, equivalent to calling the
Select.where()method.Deprecated since version 1.4: The
Exists.select().whereclauseparameter is deprecated and will be removed in version 2.0. Please make use of theSelect.where()method to add WHERE criteria to the SELECT statement.**kwargs¶ –
additional keyword arguments are passed to the legacy constructor for
Selectdescribed atSelect.create_legacy_select().Deprecated since version 1.4: The
Exists.select()method will no longer accept keyword arguments in version 2.0. Please use generative methods from theSelectconstruct in order to apply additional modifications.
See also
select()- general purpose method which allows for arbitrary column lists.
-
method
sqlalchemy.sql.expression.Exists.select_from(*froms)¶ Return a new
Existsconstruct, applying the given expression to theSelect.select_from()method of the select statement contained.Note
it is typically preferable to build a
Selectstatement first, including the desired WHERE clause, then use theSelectBase.exists()method to produce anExistsobject at once.
-
method
sqlalchemy.sql.expression.Exists.where(*clause)¶ Return a new
exists()construct with the given expression added to its WHERE clause, joined to the existing clause via AND, if any.Note
it is typically preferable to build a
Selectstatement first, including the desired WHERE clause, then use theSelectBase.exists()method to produce anExistsobject at once.
-
method
- class sqlalchemy.sql.expression.FromClause¶
Represent an element that can be used within the
FROMclause of aSELECTstatement.The most common forms of
FromClauseare theTableand theselect()constructs. Key features common to allFromClauseobjects include:a
ccollection, which provides per-name access to a collection ofColumnElementobjects.a
primary_keyattribute, which is a collection of all thoseColumnElementobjects that indicate theprimary_keyflag.Methods to generate various derivations of a “from” clause, including
FromClause.alias(),FromClause.join(),FromClause.select().
Members
alias(), c, columns, description, entity_namespace, exported_columns, foreign_keys, is_derived_from(), join(), outerjoin(), primary_key, schema, select(), table_valued(), tablesample()
Class signature
class
sqlalchemy.sql.expression.FromClause(sqlalchemy.sql.roles.AnonymizedFromClauseRole,sqlalchemy.sql.expression.Selectable)-
method
sqlalchemy.sql.expression.FromClause.alias(name=None, flat=False)¶ Return an alias of this
FromClause.E.g.:
a2 = some_table.alias('a2')
The above code creates an
Aliasobject which can be used as a FROM clause in any SELECT statement.
-
attribute
sqlalchemy.sql.expression.FromClause.c¶ A named-based collection of
ColumnElementobjects maintained by thisFromClause.The
FromClause.cattribute is an alias for theFromClause.columnsattribute.- Returns:
-
attribute
sqlalchemy.sql.expression.FromClause.columns¶ A named-based collection of
ColumnElementobjects maintained by thisFromClause.The
columns, orccollection, is the gateway to the construction of SQL expressions using table-bound or other selectable-bound columns:select(mytable).where(mytable.c.somecolumn == 5)
- Returns:
a
ColumnCollectionobject.
-
attribute
sqlalchemy.sql.expression.FromClause.description¶ A brief description of this
FromClause.Used primarily for error message formatting.
-
attribute
sqlalchemy.sql.expression.FromClause.entity_namespace¶ Return a namespace used for name-based access in SQL expressions.
This is the namespace that is used to resolve “filter_by()” type expressions, such as:
stmt.filter_by(address='some address')
It defaults to the
.ccollection, however internally it can be overridden using the “entity_namespace” annotation to deliver alternative results.
-
attribute
sqlalchemy.sql.expression.FromClause.exported_columns¶ A
ColumnCollectionthat represents the “exported” columns of thisSelectable.The “exported” columns for a
FromClauseobject are synonymous with theFromClause.columnscollection.New in version 1.4.
-
attribute
sqlalchemy.sql.expression.FromClause.foreign_keys¶ Return the collection of
ForeignKeymarker objects which this FromClause references.Each
ForeignKeyis a member of aTable-wideForeignKeyConstraint.See also
-
method
sqlalchemy.sql.expression.FromClause.is_derived_from(fromclause)¶ Return
Trueif thisFromClauseis ‘derived’ from the givenFromClause.An example would be an Alias of a Table is derived from that Table.
-
method
sqlalchemy.sql.expression.FromClause.join(right, onclause=None, isouter=False, full=False)¶ Return a
Joinfrom thisFromClauseto anotherFromClause.E.g.:
from sqlalchemy import join j = user_table.join(address_table, user_table.c.id == address_table.c.user_id) stmt = select(user_table).select_from(j)
would emit SQL along the lines of:
SELECT user.id, user.name FROM user JOIN address ON user.id = address.user_id
- Parameters:
right¶ – the right side of the join; this is any
FromClauseobject such as aTableobject, and may also be a selectable-compatible object such as an ORM-mapped class.onclause¶ – a SQL expression representing the ON clause of the join. If left at
None,FromClause.join()will attempt to join the two tables based on a foreign key relationship.isouter¶ – if True, render a LEFT OUTER JOIN, instead of JOIN.
full¶ –
if True, render a FULL OUTER JOIN, instead of LEFT OUTER JOIN. Implies
FromClause.join.isouter.New in version 1.1.
-
method
sqlalchemy.sql.expression.FromClause.outerjoin(right, onclause=None, full=False)¶ Return a
Joinfrom thisFromClauseto anotherFromClause, with the “isouter” flag set to True.E.g.:
from sqlalchemy import outerjoin j = user_table.outerjoin(address_table, user_table.c.id == address_table.c.user_id)
The above is equivalent to:
j = user_table.join( address_table, user_table.c.id == address_table.c.user_id, isouter=True)
- Parameters:
right¶ – the right side of the join; this is any
FromClauseobject such as aTableobject, and may also be a selectable-compatible object such as an ORM-mapped class.onclause¶ – a SQL expression representing the ON clause of the join. If left at
None,FromClause.join()will attempt to join the two tables based on a foreign key relationship.full¶ –
if True, render a FULL OUTER JOIN, instead of LEFT OUTER JOIN.
New in version 1.1.
-
attribute
sqlalchemy.sql.expression.FromClause.primary_key¶ Return the iterable collection of
Columnobjects which comprise the primary key of this_selectable.FromClause.For a
Tableobject, this collection is represented by thePrimaryKeyConstraintwhich itself is an iterable collection ofColumnobjects.
-
attribute
sqlalchemy.sql.expression.FromClause.schema = None¶ Define the ‘schema’ attribute for this
FromClause.This is typically
Nonefor most objects except that ofTable, where it is taken as the value of theTable.schemaargument.
-
method
sqlalchemy.sql.expression.FromClause.select(whereclause=None, **kwargs)¶ Return a SELECT of this
FromClause.e.g.:
stmt = some_table.select().where(some_table.c.id == 5)
- Parameters:
whereclause¶ –
a WHERE clause, equivalent to calling the
Select.where()method.Deprecated since version 1.4: The
FromClause.select().whereclauseparameter is deprecated and will be removed in version 2.0. Please make use of theSelect.where()method to add WHERE criteria to the SELECT statement.**kwargs¶ – additional keyword arguments are passed to the legacy constructor for
Selectdescribed atSelect.create_legacy_select().
See also
select()- general purpose method which allows for arbitrary column lists.
-
method
sqlalchemy.sql.expression.FromClause.table_valued()¶ Return a
TableValuedColumnobject for thisFromClause.A
TableValuedColumnis aColumnElementthat represents a complete row in a table. Support for this construct is backend dependent, and is supported in various forms by backends such as PostgreSQL, Oracle and SQL Server.E.g.:
>>> from sqlalchemy import select, column, func, table >>> a = table("a", column("id"), column("x"), column("y")) >>> stmt = select(func.row_to_json(a.table_valued())) >>> print(stmt) SELECT row_to_json(a) AS row_to_json_1 FROM a
New in version 1.4.0b2.
See also
Working with SQL Functions - in the SQLAlchemy 1.4 / 2.0 Tutorial
-
method
sqlalchemy.sql.expression.FromClause.tablesample(sampling, name=None, seed=None)¶ Return a TABLESAMPLE alias of this
FromClause.The return value is the
TableSampleconstruct also provided by the top-leveltablesample()function.New in version 1.1.
See also
tablesample()- usage guidelines and parameters
- class sqlalchemy.sql.expression.GenerativeSelect(_label_style=symbol('LABEL_STYLE_DISAMBIGUATE_ONLY'), use_labels=False, limit=None, offset=None, order_by=None, group_by=None, bind=None)¶
Base class for SELECT statements where additional elements can be added.
This serves as the base for
SelectandCompoundSelectwhere elements such as ORDER BY, GROUP BY can be added and column rendering can be controlled. Compare toTextualSelect, which, while it subclassesSelectBaseand is also a SELECT construct, represents a fixed textual string which cannot be altered at this level, only wrapped as a subquery.Members
apply_labels(), fetch(), get_label_style(), group_by(), limit(), offset(), order_by(), set_label_style(), slice(), with_for_update()
Class signature
class
sqlalchemy.sql.expression.GenerativeSelect(sqlalchemy.sql.expression.DeprecatedSelectBaseGenerations,sqlalchemy.sql.expression.SelectBase)-
method
sqlalchemy.sql.expression.GenerativeSelect.apply_labels()¶ Deprecated since version 1.4: The
GenerativeSelect.apply_labels()method is considered legacy as of the 1.x series of SQLAlchemy and will be removed in 2.0. Use set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) instead. (Background on SQLAlchemy 2.0 at: Migrating to SQLAlchemy 2.0)
-
method
sqlalchemy.sql.expression.GenerativeSelect.fetch(count, with_ties=False, percent=False)¶ Return a new selectable with the given FETCH FIRST criterion applied.
This is a numeric value which usually renders as
FETCH {FIRST | NEXT} [ count ] {ROW | ROWS} {ONLY | WITH TIES}expression in the resulting select. This functionality is is currently implemented for Oracle, PostgreSQL, MSSQL.Use
GenerativeSelect.offset()to specify the offset.Note
The
GenerativeSelect.fetch()method will replace any clause applied withGenerativeSelect.limit().New in version 1.4.
- Parameters:
count¶ – an integer COUNT parameter, or a SQL expression that provides an integer result. When
percent=Truethis will represent the percentage of rows to return, not the absolute value. PassNoneto reset it.with_ties¶ – When
True, the WITH TIES option is used to return any additional rows that tie for the last place in the result set according to theORDER BYclause. TheORDER BYmay be mandatory in this case. Defaults toFalsepercent¶ – When
True,countrepresents the percentage of the total number of selected rows to return. Defaults toFalse
-
method
sqlalchemy.sql.expression.GenerativeSelect.get_label_style()¶ Retrieve the current label style.
New in version 1.4.
-
method
sqlalchemy.sql.expression.GenerativeSelect.group_by(*clauses)¶ Return a new selectable with the given list of GROUP BY criterion applied.
All existing GROUP BY settings can be suppressed by passing
None.e.g.:
stmt = select(table.c.name, func.max(table.c.stat)).\ group_by(table.c.name)
- Parameters:
*clauses¶ – a series of
ColumnElementconstructs which will be used to generate an GROUP BY clause.
-
method
sqlalchemy.sql.expression.GenerativeSelect.limit(limit)¶ Return a new selectable with the given LIMIT criterion applied.
This is a numerical value which usually renders as a
LIMITexpression in the resulting select. Backends that don’t supportLIMITwill attempt to provide similar functionality.Note
The
GenerativeSelect.limit()method will replace any clause applied withGenerativeSelect.fetch().Changed in version 1.0.0: -
Select.limit()can now accept arbitrary SQL expressions as well as integer values.- Parameters:
limit¶ – an integer LIMIT parameter, or a SQL expression that provides an integer result. Pass
Noneto reset it.
-
method
sqlalchemy.sql.expression.GenerativeSelect.offset(offset)¶ Return a new selectable with the given OFFSET criterion applied.
This is a numeric value which usually renders as an
OFFSETexpression in the resulting select. Backends that don’t supportOFFSETwill attempt to provide similar functionality.Changed in version 1.0.0: -
Select.offset()can now accept arbitrary SQL expressions as well as integer values.- Parameters:
offset¶ – an integer OFFSET parameter, or a SQL expression that provides an integer result. Pass
Noneto reset it.
-
method
sqlalchemy.sql.expression.GenerativeSelect.order_by(*clauses)¶ Return a new selectable with the given list of ORDER BY criteria applied.
e.g.:
stmt = select(table).order_by(table.c.id, table.c.name)
Calling this method multiple times is equivalent to calling it once with all the clauses concatenated. All existing ORDER BY criteria may be cancelled by passing
Noneby itself. New ORDER BY criteria may then be added by invokingQuery.order_by()again, e.g.:# will erase all ORDER BY and ORDER BY new_col alone stmt = stmt.order_by(None).order_by(new_col)
- Parameters:
*clauses¶ – a series of
ColumnElementconstructs which will be used to generate an ORDER BY clause.
See also
ORDER BY - in the SQLAlchemy 1.4 / 2.0 Tutorial
Ordering or Grouping by a Label - in the SQLAlchemy 1.4 / 2.0 Tutorial
-
method
sqlalchemy.sql.expression.GenerativeSelect.set_label_style(style)¶ Return a new selectable with the specified label style.
There are three “label styles” available,
LABEL_STYLE_DISAMBIGUATE_ONLY,LABEL_STYLE_TABLENAME_PLUS_COL, andLABEL_STYLE_NONE. The default style isLABEL_STYLE_DISAMBIGUATE_ONLY.In modern SQLAlchemy, there is not generally a need to change the labeling style, as per-expression labels are more effectively used by making use of the
ColumnElement.label()method. In past versions,LABEL_STYLE_TABLENAME_PLUS_COLwas used to disambiguate same-named columns from different tables, aliases, or subqueries; the newerLABEL_STYLE_DISAMBIGUATE_ONLYnow applies labels only to names that conflict with an existing name so that the impact of this labeling is minimal.The rationale for disambiguation is mostly so that all column expressions are available from a given
FromClause.ccollection when a subquery is created.New in version 1.4: - the
GenerativeSelect.set_label_style()method replaces the previous combination of.apply_labels(),.with_labels()anduse_labels=Truemethods and/or parameters.
-
method
sqlalchemy.sql.expression.GenerativeSelect.slice(start, stop)¶ Apply LIMIT / OFFSET to this statement based on a slice.
The start and stop indices behave like the argument to Python’s built-in
range()function. This method provides an alternative to usingLIMIT/OFFSETto get a slice of the query.For example,
stmt = select(User).order_by(User).id.slice(1, 3)
renders as
SELECT users.id AS users_id, users.name AS users_name FROM users ORDER BY users.id LIMIT ? OFFSET ? (2, 1)
Note
The
GenerativeSelect.slice()method will replace any clause applied withGenerativeSelect.fetch().New in version 1.4: Added the
GenerativeSelect.slice()method generalized from the ORM.
-
method
sqlalchemy.sql.expression.GenerativeSelect.with_for_update(nowait=False, read=False, of=None, skip_locked=False, key_share=False)¶ Specify a
FOR UPDATEclause for thisGenerativeSelect.E.g.:
stmt = select(table).with_for_update(nowait=True)
On a database like PostgreSQL or Oracle, the above would render a statement like:
SELECT table.a, table.b FROM table FOR UPDATE NOWAIT
on other backends, the
nowaitoption is ignored and instead would produce:SELECT table.a, table.b FROM table FOR UPDATE
When called with no arguments, the statement will render with the suffix
FOR UPDATE. Additional arguments can then be provided which allow for common database-specific variants.- Parameters:
nowait¶ – boolean; will render
FOR UPDATE NOWAITon Oracle and PostgreSQL dialects.read¶ – boolean; will render
LOCK IN SHARE MODEon MySQL,FOR SHAREon PostgreSQL. On PostgreSQL, when combined withnowait, will renderFOR SHARE NOWAIT.of¶ – SQL expression or list of SQL expression elements (typically
Columnobjects or a compatible expression) which will render into aFOR UPDATE OFclause; supported by PostgreSQL and Oracle. May render as a table or as a column depending on backend.skip_locked¶ – boolean, will render
FOR UPDATE SKIP LOCKEDon Oracle and PostgreSQL dialects orFOR SHARE SKIP LOCKEDifread=Trueis also specified.key_share¶ – boolean, will render
FOR NO KEY UPDATE, or if combined withread=Truewill renderFOR KEY SHARE, on the PostgreSQL dialect.
-
method
- class sqlalchemy.sql.expression.HasCTE¶
Mixin that declares a class to include CTE support.
New in version 1.1.
Class signature
class
sqlalchemy.sql.expression.HasCTE(sqlalchemy.sql.roles.HasCTERole)-
method
sqlalchemy.sql.expression.HasCTE.add_cte(cte)¶ Add a
CTEto this statement object that will be independently rendered even if not referenced in the statement otherwise.This feature is useful for the use case of embedding a DML statement such as an INSERT or UPDATE as a CTE inline with a primary statement that may draw from its results indirectly; while PostgreSQL is known to support this usage, it may not be supported by other backends.
E.g.:
from sqlalchemy import table, column, select t = table('t', column('c1'), column('c2')) ins = t.insert().values({"c1": "x", "c2": "y"}).cte() stmt = select(t).add_cte(ins)
Would render:
WITH anon_1 AS (INSERT INTO t (c1, c2) VALUES (:param_1, :param_2)) SELECT t.c1, t.c2 FROM t
Above, the “anon_1” CTE is not referred towards in the SELECT statement, however still accomplishes the task of running an INSERT statement.
Similarly in a DML-related context, using the PostgreSQL
Insertconstruct to generate an “upsert”:from sqlalchemy import table, column from sqlalchemy.dialects.postgresql import insert t = table("t", column("c1"), column("c2")) delete_statement_cte = ( t.delete().where(t.c.c1 < 1).cte("deletions") ) insert_stmt = insert(t).values({"c1": 1, "c2": 2}) update_statement = insert_stmt.on_conflict_do_update( index_elements=[t.c.c1], set_={ "c1": insert_stmt.excluded.c1, "c2": insert_stmt.excluded.c2, }, ).add_cte(delete_statement_cte) print(update_statement)
The above statement renders as:
WITH deletions AS (DELETE FROM t WHERE t.c1 < %(c1_1)s) INSERT INTO t (c1, c2) VALUES (%(c1)s, %(c2)s) ON CONFLICT (c1) DO UPDATE SET c1 = excluded.c1, c2 = excluded.c2
New in version 1.4.21.
-
method
sqlalchemy.sql.expression.HasCTE.cte(name=None, recursive=False, nesting=False)¶ Return a new
CTE, or Common Table Expression instance.Common table expressions are a SQL standard whereby SELECT statements can draw upon secondary statements specified along with the primary statement, using a clause called “WITH”. Special semantics regarding UNION can also be employed to allow “recursive” queries, where a SELECT statement can draw upon the set of rows that have previously been selected.
CTEs can also be applied to DML constructs UPDATE, INSERT and DELETE on some databases, both as a source of CTE rows when combined with RETURNING, as well as a consumer of CTE rows.
Changed in version 1.1: Added support for UPDATE/INSERT/DELETE as CTE, CTEs added to UPDATE/INSERT/DELETE.
SQLAlchemy detects
CTEobjects, which are treated similarly toAliasobjects, as special elements to be delivered to the FROM clause of the statement as well as to a WITH clause at the top of the statement.For special prefixes such as PostgreSQL “MATERIALIZED” and “NOT MATERIALIZED”, the
CTE.prefix_with()method may be used to establish these.Changed in version 1.3.13: Added support for prefixes. In particular - MATERIALIZED and NOT MATERIALIZED.
- Parameters:
name¶ – name given to the common table expression. Like
FromClause.alias(), the name can be left asNonein which case an anonymous symbol will be used at query compile time.recursive¶ – if
True, will renderWITH RECURSIVE. A recursive common table expression is intended to be used in conjunction with UNION ALL in order to derive rows from those already selected.nesting¶ –
if
True, will render the CTE locally to the actual statement.New in version 1.4.24.
The following examples include two from PostgreSQL’s documentation at https://www.postgresql.org/docs/current/static/queries-with.html, as well as additional examples.
Example 1, non recursive:
from sqlalchemy import (Table, Column, String, Integer, MetaData, select, func) metadata = MetaData() orders = Table('orders', metadata, Column('region', String), Column('amount', Integer), Column('product', String), Column('quantity', Integer) ) regional_sales = select( orders.c.region, func.sum(orders.c.amount).label('total_sales') ).group_by(orders.c.region).cte("regional_sales") top_regions = select(regional_sales.c.region).\ where( regional_sales.c.total_sales > select( func.sum(regional_sales.c.total_sales) / 10 ) ).cte("top_regions") statement = select( orders.c.region, orders.c.product, func.sum(orders.c.quantity).label("product_units"), func.sum(orders.c.amount).label("product_sales") ).where(orders.c.region.in_( select(top_regions.c.region) )).group_by(orders.c.region, orders.c.product) result = conn.execute(statement).fetchall()
Example 2, WITH RECURSIVE:
from sqlalchemy import (Table, Column, String, Integer, MetaData, select, func) metadata = MetaData() parts = Table('parts', metadata, Column('part', String), Column('sub_part', String), Column('quantity', Integer), ) included_parts = select(\ parts.c.sub_part, parts.c.part, parts.c.quantity\ ).\ where(parts.c.part=='our part').\ cte(recursive=True) incl_alias = included_parts.alias() parts_alias = parts.alias() included_parts = included_parts.union_all( select( parts_alias.c.sub_part, parts_alias.c.part, parts_alias.c.quantity ).\ where(parts_alias.c.part==incl_alias.c.sub_part) ) statement = select( included_parts.c.sub_part, func.sum(included_parts.c.quantity). label('total_quantity') ).\ group_by(included_parts.c.sub_part) result = conn.execute(statement).fetchall()
Example 3, an upsert using UPDATE and INSERT with CTEs:
from datetime import date from sqlalchemy import (MetaData, Table, Column, Integer, Date, select, literal, and_, exists) metadata = MetaData() visitors = Table('visitors', metadata, Column('product_id', Integer, primary_key=True), Column('date', Date, primary_key=True), Column('count', Integer), ) # add 5 visitors for the product_id == 1 product_id = 1 day = date.today() count = 5 update_cte = ( visitors.update() .where(and_(visitors.c.product_id == product_id, visitors.c.date == day)) .values(count=visitors.c.count + count) .returning(literal(1)) .cte('update_cte') ) upsert = visitors.insert().from_select( [visitors.c.product_id, visitors.c.date, visitors.c.count], select(literal(product_id), literal(day), literal(count)) .where(~exists(update_cte.select())) ) connection.execute(upsert)
Example 4, Nesting CTE (SQLAlchemy 1.4.24 and above):
value_a = select( literal("root").label("n") ).cte("value_a") # A nested CTE with the same name as the root one value_a_nested = select( literal("nesting").label("n") ).cte("value_a", nesting=True) # Nesting CTEs takes ascendency locally # over the CTEs at a higher level value_b = select(value_a_nested.c.n).cte("value_b") value_ab = select(value_a.c.n.label("a"), value_b.c.n.label("b"))
The above query will render the second CTE nested inside the first, shown with inline parameters below as:
WITH value_a AS (SELECT 'root' AS n), value_b AS (WITH value_a AS (SELECT 'nesting' AS n) SELECT value_a.n AS n FROM value_a) SELECT value_a.n AS a, value_b.n AS b FROM value_a, value_b
Example 5, Non-Linear CTE (SQLAlchemy 1.4.28 and above):
edge = Table( "edge", metadata, Column("id", Integer, primary_key=True), Column("left", Integer), Column("right", Integer), ) root_node = select(literal(1).label("node")).cte( "nodes", recursive=True ) left_edge = select(edge.c.left).join( root_node, edge.c.right == root_node.c.node ) right_edge = select(edge.c.right).join( root_node, edge.c.left == root_node.c.node ) subgraph_cte = root_node.union(left_edge, right_edge) subgraph = select(subgraph_cte)
The above query will render 2 UNIONs inside the recursive CTE:
WITH RECURSIVE nodes(node) AS ( SELECT 1 AS node UNION SELECT edge."left" AS "left" FROM edge JOIN nodes ON edge."right" = nodes.node UNION SELECT edge."right" AS "right" FROM edge JOIN nodes ON edge."left" = nodes.node ) SELECT nodes.node FROM nodes
See also
Query.cte()- ORM version ofHasCTE.cte().
-
method
- class sqlalchemy.sql.expression.HasPrefixes¶
Members
-
method
sqlalchemy.sql.expression.HasPrefixes.prefix_with(*expr, **kw)¶ Add one or more expressions following the statement keyword, i.e. SELECT, INSERT, UPDATE, or DELETE. Generative.
This is used to support backend-specific prefix keywords such as those provided by MySQL.
E.g.:
stmt = table.insert().prefix_with("LOW_PRIORITY", dialect="mysql") # MySQL 5.7 optimizer hints stmt = select(table).prefix_with( "/*+ BKA(t1) */", dialect="mysql")
Multiple prefixes can be specified by multiple calls to
HasPrefixes.prefix_with().- Parameters:
*expr¶ – textual or
ClauseElementconstruct which will be rendered following the INSERT, UPDATE, or DELETE keyword.**kw¶ – A single keyword ‘dialect’ is accepted. This is an optional string dialect name which will limit rendering of this prefix to only that dialect.
-
method
- class sqlalchemy.sql.expression.HasSuffixes¶
Members
-
method
sqlalchemy.sql.expression.HasSuffixes.suffix_with(*expr, **kw)¶ Add one or more expressions following the statement as a whole.
This is used to support backend-specific suffix keywords on certain constructs.
E.g.:
stmt = select(col1, col2).cte().suffix_with( "cycle empno set y_cycle to 1 default 0", dialect="oracle")
Multiple suffixes can be specified by multiple calls to
HasSuffixes.suffix_with().- Parameters:
*expr¶ – textual or
ClauseElementconstruct which will be rendered following the target clause.**kw¶ – A single keyword ‘dialect’ is accepted. This is an optional string dialect name which will limit rendering of this suffix to only that dialect.
-
method
- class sqlalchemy.sql.expression.Join(left, right, onclause=None, isouter=False, full=False)¶
Represent a
JOINconstruct between twoFromClauseelements.The public constructor function for
Joinis the module-leveljoin()function, as well as theFromClause.join()method of anyFromClause(e.g. such asTable).Members
__init__(), alias(), bind, description, is_derived_from(), select(), self_group()
Class signature
class
sqlalchemy.sql.expression.Join(sqlalchemy.sql.roles.DMLTableRole,sqlalchemy.sql.expression.FromClause)-
method
sqlalchemy.sql.expression.Join.__init__(left, right, onclause=None, isouter=False, full=False)¶ Construct a new
Join.The usual entrypoint here is the
join()function or theFromClause.join()method of anyFromClauseobject.
-
method
sqlalchemy.sql.expression.Join.alias(name=None, flat=False)¶ Return an alias of this
Join.Deprecated since version 1.4: The
Join.alias()method is considered legacy as of the 1.x series of SQLAlchemy and will be removed in 2.0. Create a select + subquery, or alias the individual tables inside the join, instead. (Background on SQLAlchemy 2.0 at: Migrating to SQLAlchemy 2.0)The default behavior here is to first produce a SELECT construct from this
Join, then to produce anAliasfrom that. So given a join of the form:j = table_a.join(table_b, table_a.c.id == table_b.c.a_id)
The JOIN by itself would look like:
table_a JOIN table_b ON table_a.id = table_b.a_id
Whereas the alias of the above,
j.alias(), would in a SELECT context look like:(SELECT table_a.id AS table_a_id, table_b.id AS table_b_id, table_b.a_id AS table_b_a_id FROM table_a JOIN table_b ON table_a.id = table_b.a_id) AS anon_1
The equivalent long-hand form, given a
Joinobjectj, is:from sqlalchemy import select, alias j = alias( select(j.left, j.right).\ select_from(j).\ set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL).\ correlate(False), name=name )
The selectable produced by
Join.alias()features the same columns as that of the two individual selectables presented under a single name - the individual columns are “auto-labeled”, meaning the.c.collection of the resultingAliasrepresents the names of the individual columns using a<tablename>_<columname>scheme:j.c.table_a_id j.c.table_b_a_id
Join.alias()also features an alternate option for aliasing joins which produces no enclosing SELECT and does not normally apply labels to the column names. Theflat=Trueoption will callFromClause.alias()against the left and right sides individually. Using this option, no newSELECTis produced; we instead, from a construct as below:j = table_a.join(table_b, table_a.c.id == table_b.c.a_id) j = j.alias(flat=True)
we get a result like this:
table_a AS table_a_1 JOIN table_b AS table_b_1 ON table_a_1.id = table_b_1.a_id
The
flat=Trueargument is also propagated to the contained selectables, so that a composite join such as:j = table_a.join( table_b.join(table_c, table_b.c.id == table_c.c.b_id), table_b.c.a_id == table_a.c.id ).alias(flat=True)
Will produce an expression like:
table_a AS table_a_1 JOIN ( table_b AS table_b_1 JOIN table_c AS table_c_1 ON table_b_1.id = table_c_1.b_id ) ON table_a_1.id = table_b_1.a_id
The standalone
alias()function as well as the baseFromClause.alias()method also support theflat=Trueargument as a no-op, so that the argument can be passed to thealias()method of any selectable.
-
attribute
sqlalchemy.sql.expression.Join.bind¶ Return the bound engine associated with either the left or right side of this
Join.Deprecated since version 1.4: The
Executable.bindattribute is considered legacy as of the 1.x series of SQLAlchemy and will be removed in 2.0. Bound metadata is being removed as of SQLAlchemy 2.0. (Background on SQLAlchemy 2.0 at: Migrating to SQLAlchemy 2.0)
-
attribute
sqlalchemy.sql.expression.Join.description¶
-
method
sqlalchemy.sql.expression.Join.is_derived_from(fromclause)¶ Return
Trueif thisFromClauseis ‘derived’ from the givenFromClause.An example would be an Alias of a Table is derived from that Table.
-
method
sqlalchemy.sql.expression.Join.select(whereclause=None, **kwargs)¶ Create a
Selectfrom thisJoin.E.g.:
stmt = table_a.join(table_b, table_a.c.id == table_b.c.a_id) stmt = stmt.select()
The above will produce a SQL string resembling:
SELECT table_a.id, table_a.col, table_b.id, table_b.a_id FROM table_a JOIN table_b ON table_a.id = table_b.a_id
- Parameters:
whereclause¶ –
WHERE criteria, same as calling
Select.where()on the resulting statementDeprecated since version 1.4: The
Join.select().whereclauseparameter is deprecated and will be removed in version 2.0. Please make use of theSelect.where()method to add WHERE criteria to the SELECT statement.**kwargs¶ – additional keyword arguments are passed to the legacy constructor for
Selectdescribed atSelect.create_legacy_select().
-
method
sqlalchemy.sql.expression.Join.self_group(against=None)¶ Apply a ‘grouping’ to this
ClauseElement.This method is overridden by subclasses to return a “grouping” construct, i.e. parenthesis. In particular it’s used by “binary” expressions to provide a grouping around themselves when placed into a larger expression, as well as by
select()constructs when placed into the FROM clause of anotherselect(). (Note that subqueries should be normally created using theSelect.alias()method, as many platforms require nested SELECT statements to be named).As expressions are composed together, the application of
self_group()is automatic - end-user code should never need to use this method directly. Note that SQLAlchemy’s clause constructs take operator precedence into account - so parenthesis might not be needed, for example, in an expression likex OR (y AND z)- AND takes precedence over OR.The base
self_group()method ofClauseElementjust returns self.
-
method
- class sqlalchemy.sql.expression.Lateral(*arg, **kw)¶
Represent a LATERAL subquery.
This object is constructed from the
lateral()module level function as well as theFromClause.lateral()method available on allFromClausesubclasses.While LATERAL is part of the SQL standard, currently only more recent PostgreSQL versions provide support for this keyword.
New in version 1.1.
See also
LATERAL correlation - overview of usage.
Members
Class signature
class
sqlalchemy.sql.expression.Lateral(sqlalchemy.sql.expression.AliasedReturnsRows)-
attribute
sqlalchemy.sql.expression.Lateral.inherit_cache = True¶ Indicate if this
HasCacheKeyinstance should make use of the cache key generation scheme used by its immediate superclass.The attribute defaults to
None, which indicates that a construct has not yet taken into account whether or not its appropriate for it to participate in caching; this is functionally equivalent to setting the value toFalse, except that a warning is also emitted.This flag can be set to
Trueon a particular class, if the SQL that corresponds to the object does not change based on attributes which are local to this class, and not its superclass.See also
Enabling Caching Support for Custom Constructs - General guideslines for setting the
HasCacheKey.inherit_cacheattribute for third-party or user defined SQL constructs.
-
attribute
- class sqlalchemy.sql.expression.ReturnsRows¶
The base-most class for Core constructs that have some concept of columns that can represent rows.
While the SELECT statement and TABLE are the primary things we think of in this category, DML like INSERT, UPDATE and DELETE can also specify RETURNING which means they can be used in CTEs and other forms, and PostgreSQL has functions that return rows also.
New in version 1.4.
Members
Class signature
class
sqlalchemy.sql.expression.ReturnsRows(sqlalchemy.sql.roles.ReturnsRowsRole,sqlalchemy.sql.expression.ClauseElement)-
attribute
sqlalchemy.sql.expression.ReturnsRows.exported_columns¶ A
ColumnCollectionthat represents the “exported” columns of thisReturnsRows.The “exported” columns represent the collection of
ColumnElementexpressions that are rendered by this SQL construct. There are primary varieties which are the “FROM clause columns” of a FROM clause, such as a table, join, or subquery, the “SELECTed columns”, which are the columns in the “columns clause” of a SELECT statement, and the RETURNING columns in a DML statement..New in version 1.4.
-
attribute
- class sqlalchemy.sql.expression.ScalarSelect(element)¶
Represent a scalar subquery.
A
ScalarSelectis created by invoking theSelectBase.scalar_subquery()method. The object then participates in other SQL expressions as a SQL column expression within theColumnElementhierarchy.Members
correlate(), correlate_except(), inherit_cache, self_group(), where()
Class signature
class
sqlalchemy.sql.expression.ScalarSelect(sqlalchemy.sql.roles.InElementRole,sqlalchemy.sql.expression.Generative,sqlalchemy.sql.expression.Grouping)-
method
sqlalchemy.sql.expression.ScalarSelect.correlate(*fromclauses)¶ Return a new
ScalarSelectwhich will correlate the given FROM clauses to that of an enclosingSelect.This method is mirrored from the
Select.correlate()method of the underlyingSelect. The method applies the :meth:_sql.Select.correlate` method, then returns a newScalarSelectagainst that statement.New in version 1.4: Previously, the
ScalarSelect.correlate()method was only available fromSelect.- Parameters:
*fromclauses¶ – a list of one or more
FromClauseconstructs, or other compatible constructs (i.e. ORM-mapped classes) to become part of the correlate collection.
-
method
sqlalchemy.sql.expression.ScalarSelect.correlate_except(*fromclauses)¶ Return a new
ScalarSelectwhich will omit the given FROM clauses from the auto-correlation process.This method is mirrored from the
Select.correlate_except()method of the underlyingSelect. The method applies the :meth:_sql.Select.correlate_except` method, then returns a newScalarSelectagainst that statement.New in version 1.4: Previously, the
ScalarSelect.correlate_except()method was only available fromSelect.- Parameters:
*fromclauses¶ – a list of one or more
FromClauseconstructs, or other compatible constructs (i.e. ORM-mapped classes) to become part of the correlate-exception collection.
-
attribute
sqlalchemy.sql.expression.ScalarSelect.inherit_cache = True¶ Indicate if this
HasCacheKeyinstance should make use of the cache key generation scheme used by its immediate superclass.The attribute defaults to
None, which indicates that a construct has not yet taken into account whether or not its appropriate for it to participate in caching; this is functionally equivalent to setting the value toFalse, except that a warning is also emitted.This flag can be set to
Trueon a particular class, if the SQL that corresponds to the object does not change based on attributes which are local to this class, and not its superclass.See also
Enabling Caching Support for Custom Constructs - General guideslines for setting the
HasCacheKey.inherit_cacheattribute for third-party or user defined SQL constructs.
-
method
sqlalchemy.sql.expression.ScalarSelect.self_group(**kwargs)¶ Apply a ‘grouping’ to this
ClauseElement.This method is overridden by subclasses to return a “grouping” construct, i.e. parenthesis. In particular it’s used by “binary” expressions to provide a grouping around themselves when placed into a larger expression, as well as by
select()constructs when placed into the FROM clause of anotherselect(). (Note that subqueries should be normally created using theSelect.alias()method, as many platforms require nested SELECT statements to be named).As expressions are composed together, the application of
self_group()is automatic - end-user code should never need to use this method directly. Note that SQLAlchemy’s clause constructs take operator precedence into account - so parenthesis might not be needed, for example, in an expression likex OR (y AND z)- AND takes precedence over OR.The base
self_group()method ofClauseElementjust returns self.
-
method
sqlalchemy.sql.expression.ScalarSelect.where(crit)¶ Apply a WHERE clause to the SELECT statement referred to by this
ScalarSelect.
-
method
- class sqlalchemy.sql.expression.Select¶
Represents a
SELECTstatement.The
Selectobject is normally constructed using theselect()function. See that function for details.Members
add_columns(), add_cte(), alias(), apply_labels(), as_scalar(), bind, c, column(), column_descriptions, columns_clause_froms, correlate(), correlate_except(), corresponding_column(), create_legacy_select(), cte(), distinct(), except_(), except_all(), execute(), execution_options(), exists(), exported_columns, fetch(), filter(), filter_by(), from_statement(), froms, get_children(), get_execution_options(), get_final_froms(), get_label_style(), group_by(), having(), inner_columns, intersect(), intersect_all(), join(), join_from(), label(), lateral(), limit(), offset(), options(), order_by(), outerjoin(), outerjoin_from(), prefix_with(), reduce_columns(), replace_selectable(), scalar(), scalar_subquery(), select(), select_from(), selected_columns, self_group(), set_label_style(), slice(), subquery(), suffix_with(), union(), union_all(), where(), whereclause, with_for_update(), with_hint(), with_only_columns(), with_statement_hint()
Class signature
class
sqlalchemy.sql.expression.Select(sqlalchemy.sql.expression.HasPrefixes,sqlalchemy.sql.expression.HasSuffixes,sqlalchemy.sql.expression.HasHints,sqlalchemy.sql.expression.HasCompileState,sqlalchemy.sql.expression.DeprecatedSelectGenerations,sqlalchemy.sql.expression._SelectFromElements,sqlalchemy.sql.expression.GenerativeSelect)-
method
sqlalchemy.sql.expression.Select.add_columns(*columns)¶ Return a new
select()construct with the given column expressions added to its columns clause.E.g.:
my_select = my_select.add_columns(table.c.new_column)
See the documentation for
Select.with_only_columns()for guidelines on adding /replacing the columns of aSelectobject.
-
method
sqlalchemy.sql.expression.Select.add_cte(cte)¶ inherited from the
HasCTE.add_cte()method ofHasCTEAdd a
CTEto this statement object that will be independently rendered even if not referenced in the statement otherwise.This feature is useful for the use case of embedding a DML statement such as an INSERT or UPDATE as a CTE inline with a primary statement that may draw from its results indirectly; while PostgreSQL is known to support this usage, it may not be supported by other backends.
E.g.:
from sqlalchemy import table, column, select t = table('t', column('c1'), column('c2')) ins = t.insert().values({"c1": "x", "c2": "y"}).cte() stmt = select(t).add_cte(ins)
Would render:
WITH anon_1 AS (INSERT INTO t (c1, c2) VALUES (:param_1, :param_2)) SELECT t.c1, t.c2 FROM t
Above, the “anon_1” CTE is not referred towards in the SELECT statement, however still accomplishes the task of running an INSERT statement.
Similarly in a DML-related context, using the PostgreSQL
Insertconstruct to generate an “upsert”:from sqlalchemy import table, column from sqlalchemy.dialects.postgresql import insert t = table("t", column("c1"), column("c2")) delete_statement_cte = ( t.delete().where(t.c.c1 < 1).cte("deletions") ) insert_stmt = insert(t).values({"c1": 1, "c2": 2}) update_statement = insert_stmt.on_conflict_do_update( index_elements=[t.c.c1], set_={ "c1": insert_stmt.excluded.c1, "c2": insert_stmt.excluded.c2, }, ).add_cte(delete_statement_cte) print(update_statement)
The above statement renders as:
WITH deletions AS (DELETE FROM t WHERE t.c1 < %(c1_1)s) INSERT INTO t (c1, c2) VALUES (%(c1)s, %(c2)s) ON CONFLICT (c1) DO UPDATE SET c1 = excluded.c1, c2 = excluded.c2
New in version 1.4.21.
-
method
sqlalchemy.sql.expression.Select.alias(name=None, flat=False)¶ inherited from the
SelectBase.alias()method ofSelectBaseReturn a named subquery against this
SelectBase.For a
SelectBase(as opposed to aFromClause), this returns aSubqueryobject which behaves mostly the same as theAliasobject that is used with aFromClause.Changed in version 1.4: The
SelectBase.alias()method is now a synonym for theSelectBase.subquery()method.
-
method
sqlalchemy.sql.expression.Select.apply_labels()¶ inherited from the
GenerativeSelect.apply_labels()method ofGenerativeSelectDeprecated since version 1.4: The
GenerativeSelect.apply_labels()method is considered legacy as of the 1.x series of SQLAlchemy and will be removed in 2.0. Use set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) instead. (Background on SQLAlchemy 2.0 at: Migrating to SQLAlchemy 2.0)
-
method
sqlalchemy.sql.expression.Select.as_scalar()¶ inherited from the
SelectBase.as_scalar()method ofSelectBaseDeprecated since version 1.4: The
SelectBase.as_scalar()method is deprecated and will be removed in a future release. Please refer toSelectBase.scalar_subquery().
-
attribute
sqlalchemy.sql.expression.Select.bind¶ Returns the
EngineorConnectionto which thisExecutableis bound, or None if none found.Deprecated since version 1.4: The
Executable.bindattribute is considered legacy as of the 1.x series of SQLAlchemy and will be removed in 2.0. Bound metadata is being removed as of SQLAlchemy 2.0. (Background on SQLAlchemy 2.0 at: Migrating to SQLAlchemy 2.0)
-
attribute
sqlalchemy.sql.expression.Select.c¶ inherited from the
SelectBase.cattribute ofSelectBaseDeprecated since version 1.4: The
SelectBase.candSelectBase.columnsattributes are deprecated and will be removed in a future release; these attributes implicitly create a subquery that should be explicit. Please callSelectBase.subquery()first in order to create a subquery, which then contains this attribute. To access the columns that this SELECT object SELECTs from, use theSelectBase.selected_columnsattribute.
-
method
sqlalchemy.sql.expression.Select.column(column)¶ Return a new
select()construct with the given column expression added to its columns clause.Deprecated since version 1.4: The
Select.column()method is deprecated and will be removed in a future release. Please useSelect.add_columns()E.g.:
my_select = my_select.column(table.c.new_column)
See the documentation for
Select.with_only_columns()for guidelines on adding /replacing the columns of aSelectobject.
-
attribute
sqlalchemy.sql.expression.Select.column_descriptions¶ Return a plugin-enabled ‘column descriptions’ structure referring to the columns which are SELECTed by this statement.
This attribute is generally useful when using the ORM, as an extended structure which includes information about mapped entities is returned. The section Inspecting entities and columns from ORM-enabled SELECT and DML statements contains more background.
For a Core-only statement, the structure returned by this accessor is derived from the same objects that are returned by the
Select.selected_columnsaccessor, formatted as a list of dictionaries which contain the keysname,typeandexpr, which indicate the column expressions to be selected:>>> stmt = select(user_table) >>> stmt.column_descriptions [ { 'name': 'id', 'type': Integer(), 'expr': Column('id', Integer(), ...)}, { 'name': 'name', 'type': String(length=30), 'expr': Column('name', String(length=30), ...)} ]
Changed in version 1.4.33: The
Select.column_descriptionsattribute returns a structure for a Core-only set of entities, not just ORM-only entities.See also
UpdateBase.entity_description- entity information for aninsert(),update(), ordelete()Inspecting entities and columns from ORM-enabled SELECT and DML statements - ORM background
-
attribute
sqlalchemy.sql.expression.Select.columns_clause_froms¶ Return the set of
FromClauseobjects implied by the columns clause of this SELECT statement.New in version 1.4.23.
See also
Select.froms- “final” FROM list taking the full statement into accountSelect.with_only_columns()- makes use of this collection to set up a new FROM list
-
method
sqlalchemy.sql.expression.Select.correlate(*fromclauses)¶ Return a new
Selectwhich will correlate the given FROM clauses to that of an enclosingSelect.Calling this method turns off the
Selectobject’s default behavior of “auto-correlation”. Normally, FROM elements which appear in aSelectthat encloses this one via its WHERE clause, ORDER BY, HAVING or columns clause will be omitted from thisSelectobject’s FROM clause. Setting an explicit correlation collection using theSelect.correlate()method provides a fixed list of FROM objects that can potentially take place in this process.When
Select.correlate()is used to apply specific FROM clauses for correlation, the FROM elements become candidates for correlation regardless of how deeply nested thisSelectobject is, relative to an enclosingSelectwhich refers to the same FROM object. This is in contrast to the behavior of “auto-correlation” which only correlates to an immediate enclosingSelect. Multi-level correlation ensures that the link between enclosed and enclosingSelectis always via at least one WHERE/ORDER BY/HAVING/columns clause in order for correlation to take place.If
Noneis passed, theSelectobject will correlate none of its FROM entries, and all will render unconditionally in the local FROM clause.- Parameters:
*fromclauses¶ – a list of one or more
FromClauseconstructs, or other compatible constructs (i.e. ORM-mapped classes) to become part of the correlate collection.
-
method
sqlalchemy.sql.expression.Select.correlate_except(*fromclauses)¶ Return a new
Selectwhich will omit the given FROM clauses from the auto-correlation process.Calling
Select.correlate_except()turns off theSelectobject’s default behavior of “auto-correlation” for the given FROM elements. An element specified here will unconditionally appear in the FROM list, while all other FROM elements remain subject to normal auto-correlation behaviors.If
Noneis passed, theSelectobject will correlate all of its FROM entries.- Parameters:
*fromclauses¶ – a list of one or more
FromClauseconstructs, or other compatible constructs (i.e. ORM-mapped classes) to become part of the correlate-exception collection.
-
method
sqlalchemy.sql.expression.Select.corresponding_column(column, require_embedded=False)¶ inherited from the
Selectable.corresponding_column()method ofSelectableGiven a
ColumnElement, return the exportedColumnElementobject from theSelectable.exported_columnscollection of thisSelectablewhich corresponds to that originalColumnElementvia a common ancestor column.- Parameters:
column¶ – the target
ColumnElementto be matched.require_embedded¶ – only return corresponding columns for the given
ColumnElement, if the givenColumnElementis actually present within a sub-element of thisSelectable. Normally the column will match if it merely shares a common ancestor with one of the exported columns of thisSelectable.
See also
Selectable.exported_columns- theColumnCollectionthat is used for the operation.ColumnCollection.corresponding_column()- implementation method.
-
classmethod
sqlalchemy.sql.expression.Select.create_legacy_select(columns=None, whereclause=None, from_obj=None, distinct=False, having=None, correlate=True, prefixes=None, suffixes=None, **kwargs)¶ Construct a new
Selectusing the 1.x style API.Deprecated since version 1.4: The legacy calling style of
select()is deprecated and will be removed in SQLAlchemy 2.0. Please use the new calling style described atselect(). (Background on SQLAlchemy 2.0 at: Migrating to SQLAlchemy 2.0)This method is called implicitly when the
select()construct is used and the first argument is a Python list or other plain sequence object, which is taken to refer to the columns collection.Changed in version 1.4: Added the
Select.create_legacy_select()constructor which documents the calling style in use when theselect()construct is invoked using 1.x-style arguments.Similar functionality is also available via the
FromClause.select()method on anyFromClause.All arguments which accept
ClauseElementarguments also accept string arguments, which will be converted as appropriate into eithertext()orliteral_column()constructs.See also
Selecting Rows with Core or ORM - in the SQLAlchemy 1.4 / 2.0 Tutorial
- Parameters:
columns¶ –
A list of
ColumnElementorFromClauseobjects which will form the columns clause of the resulting statement. For those objects that are instances ofFromClause(typicallyTableorAliasobjects), theFromClause.ccollection is extracted to form a collection ofColumnElementobjects.This parameter will also accept
TextClauseconstructs as given, as well as ORM-mapped classes.Note
The
select.columnsparameter is not available in the method form ofselect(), e.g.FromClause.select().whereclause¶ –
A
ClauseElementexpression which will be used to form theWHEREclause. It is typically preferable to add WHERE criterion to an existingSelectusing method chaining withSelect.where().See also
from_obj¶ –
A list of
ClauseElementobjects which will be added to theFROMclause of the resulting statement. This is equivalent to callingSelect.select_from()using method chaining on an existingSelectobject.See also
Select.select_from()- full description of explicit FROM clause specification.bind=None¶ – an
EngineorConnectioninstance to which the resultingSelectobject will be bound. TheSelectobject will otherwise automatically bind to whateverConnectableinstances can be located within its containedClauseElementmembers.correlate=True¶ –
indicates that this
Selectobject should have its containedFromClauseelements “correlated” to an enclosingSelectobject. It is typically preferable to specify correlations on an existingSelectconstruct usingSelect.correlate().See also
Select.correlate()- full description of correlation.distinct=False¶ –
when
True, applies aDISTINCTqualifier to the columns clause of the resulting statement.The boolean argument may also be a column expression or list of column expressions - this is a special calling form which is understood by the PostgreSQL dialect to render the
DISTINCT ON (<columns>)syntax.distinctis also available on an existingSelectobject via theSelect.distinct()method.See also
group_by¶ –
a list of
ClauseElementobjects which will comprise theGROUP BYclause of the resulting select. This parameter is typically specified more naturally using theSelect.group_by()method on an existingSelect.See also
having¶ –
a
ClauseElementthat will comprise theHAVINGclause of the resulting select whenGROUP BYis used. This parameter is typically specified more naturally using theSelect.having()method on an existingSelect.See also
limit=None¶ –
a numerical value which usually renders as a
LIMITexpression in the resulting select. Backends that don’t supportLIMITwill attempt to provide similar functionality. This parameter is typically specified more naturally using theSelect.limit()method on an existingSelect.See also
offset=None¶ –
a numeric value which usually renders as an
OFFSETexpression in the resulting select. Backends that don’t supportOFFSETwill attempt to provide similar functionality. This parameter is typically specified more naturally using theSelect.offset()method on an existingSelect.See also
order_by¶ –
a scalar or list of
ClauseElementobjects which will comprise theORDER BYclause of the resulting select. This parameter is typically specified more naturally using theSelect.order_by()method on an existingSelect.See also
use_labels=False¶ –
when
True, the statement will be generated using labels for each column in the columns clause, which qualify each column with its parent table’s (or aliases) name so that name conflicts between columns in different tables don’t occur. The format of the label is<tablename>_<column>. The “c” collection of aSubquerycreated against thisSelectobject, as well as theSelect.selected_columnscollection of theSelectitself, will use these names for targeting column members.This parameter can also be specified on an existing
Selectobject using theSelect.set_label_style()method.See also
-
method
sqlalchemy.sql.expression.Select.cte(name=None, recursive=False, nesting=False)¶ inherited from the
HasCTE.cte()method ofHasCTEReturn a new
CTE, or Common Table Expression instance.Common table expressions are a SQL standard whereby SELECT statements can draw upon secondary statements specified along with the primary statement, using a clause called “WITH”. Special semantics regarding UNION can also be employed to allow “recursive” queries, where a SELECT statement can draw upon the set of rows that have previously been selected.
CTEs can also be applied to DML constructs UPDATE, INSERT and DELETE on some databases, both as a source of CTE rows when combined with RETURNING, as well as a consumer of CTE rows.
Changed in version 1.1: Added support for UPDATE/INSERT/DELETE as CTE, CTEs added to UPDATE/INSERT/DELETE.
SQLAlchemy detects
CTEobjects, which are treated similarly toAliasobjects, as special elements to be delivered to the FROM clause of the statement as well as to a WITH clause at the top of the statement.For special prefixes such as PostgreSQL “MATERIALIZED” and “NOT MATERIALIZED”, the
CTE.prefix_with()method may be used to establish these.Changed in version 1.3.13: Added support for prefixes. In particular - MATERIALIZED and NOT MATERIALIZED.
- Parameters:
name¶ – name given to the common table expression. Like
FromClause.alias(), the name can be left asNonein which case an anonymous symbol will be used at query compile time.recursive¶ – if
True, will renderWITH RECURSIVE. A recursive common table expression is intended to be used in conjunction with UNION ALL in order to derive rows from those already selected.nesting¶ –
if
True, will render the CTE locally to the actual statement.New in version 1.4.24.
The following examples include two from PostgreSQL’s documentation at https://www.postgresql.org/docs/current/static/queries-with.html, as well as additional examples.
Example 1, non recursive:
from sqlalchemy import (Table, Column, String, Integer, MetaData, select, func) metadata = MetaData() orders = Table('orders', metadata, Column('region', String), Column('amount', Integer), Column('product', String), Column('quantity', Integer) ) regional_sales = select( orders.c.region, func.sum(orders.c.amount).label('total_sales') ).group_by(orders.c.region).cte("regional_sales") top_regions = select(regional_sales.c.region).\ where( regional_sales.c.total_sales > select( func.sum(regional_sales.c.total_sales) / 10 ) ).cte("top_regions") statement = select( orders.c.region, orders.c.product, func.sum(orders.c.quantity).label("product_units"), func.sum(orders.c.amount).label("product_sales") ).where(orders.c.region.in_( select(top_regions.c.region) )).group_by(orders.c.region, orders.c.product) result = conn.execute(statement).fetchall()
Example 2, WITH RECURSIVE:
from sqlalchemy import (Table, Column, String, Integer, MetaData, select, func) metadata = MetaData() parts = Table('parts', metadata, Column('part', String), Column('sub_part', String), Column('quantity', Integer), ) included_parts = select(\ parts.c.sub_part, parts.c.part, parts.c.quantity\ ).\ where(parts.c.part=='our part').\ cte(recursive=True) incl_alias = included_parts.alias() parts_alias = parts.alias() included_parts = included_parts.union_all( select( parts_alias.c.sub_part, parts_alias.c.part, parts_alias.c.quantity ).\ where(parts_alias.c.part==incl_alias.c.sub_part) ) statement = select( included_parts.c.sub_part, func.sum(included_parts.c.quantity). label('total_quantity') ).\ group_by(included_parts.c.sub_part) result = conn.execute(statement).fetchall()
Example 3, an upsert using UPDATE and INSERT with CTEs:
from datetime import date from sqlalchemy import (MetaData, Table, Column, Integer, Date, select, literal, and_, exists) metadata = MetaData() visitors = Table('visitors', metadata, Column('product_id', Integer, primary_key=True), Column('date', Date, primary_key=True), Column('count', Integer), ) # add 5 visitors for the product_id == 1 product_id = 1 day = date.today() count = 5 update_cte = ( visitors.update() .where(and_(visitors.c.product_id == product_id, visitors.c.date == day)) .values(count=visitors.c.count + count) .returning(literal(1)) .cte('update_cte') ) upsert = visitors.insert().from_select( [visitors.c.product_id, visitors.c.date, visitors.c.count], select(literal(product_id), literal(day), literal(count)) .where(~exists(update_cte.select())) ) connection.execute(upsert)
Example 4, Nesting CTE (SQLAlchemy 1.4.24 and above):
value_a = select( literal("root").label("n") ).cte("value_a") # A nested CTE with the same name as the root one value_a_nested = select( literal("nesting").label("n") ).cte("value_a", nesting=True) # Nesting CTEs takes ascendency locally # over the CTEs at a higher level value_b = select(value_a_nested.c.n).cte("value_b") value_ab = select(value_a.c.n.label("a"), value_b.c.n.label("b"))
The above query will render the second CTE nested inside the first, shown with inline parameters below as:
WITH value_a AS (SELECT 'root' AS n), value_b AS (WITH value_a AS (SELECT 'nesting' AS n) SELECT value_a.n AS n FROM value_a) SELECT value_a.n AS a, value_b.n AS b FROM value_a, value_b
Example 5, Non-Linear CTE (SQLAlchemy 1.4.28 and above):
edge = Table( "edge", metadata, Column("id", Integer, primary_key=True), Column("left", Integer), Column("right", Integer), ) root_node = select(literal(1).label("node")).cte( "nodes", recursive=True ) left_edge = select(edge.c.left).join( root_node, edge.c.right == root_node.c.node ) right_edge = select(edge.c.right).join( root_node, edge.c.left == root_node.c.node ) subgraph_cte = root_node.union(left_edge, right_edge) subgraph = select(subgraph_cte)
The above query will render 2 UNIONs inside the recursive CTE:
WITH RECURSIVE nodes(node) AS ( SELECT 1 AS node UNION SELECT edge."left" AS "left" FROM edge JOIN nodes ON edge."right" = nodes.node UNION SELECT edge."right" AS "right" FROM edge JOIN nodes ON edge."left" = nodes.node ) SELECT nodes.node FROM nodes
See also
Query.cte()- ORM version ofHasCTE.cte().
-
method
sqlalchemy.sql.expression.Select.distinct(*expr)¶ Return a new
select()construct which will apply DISTINCT to its columns clause.- Parameters:
*expr¶ –
optional column expressions. When present, the PostgreSQL dialect will render a
DISTINCT ON (<expressions>>)construct.Deprecated since version 1.4: Using *expr in other dialects is deprecated and will raise
CompileErrorin a future version.
-
method
sqlalchemy.sql.expression.Select.except_(*other, **kwargs)¶ Return a SQL
EXCEPTof this select() construct against the given selectable provided as positional arguments.- Parameters:
*other¶ –
one or more elements with which to create a UNION.
Changed in version 1.4.28: multiple elements are now accepted.
**kwargs¶ – keyword arguments are forwarded to the constructor for the newly created
CompoundSelectobject.
-
method
sqlalchemy.sql.expression.Select.except_all(*other, **kwargs)¶ Return a SQL
EXCEPT ALLof this select() construct against the given selectables provided as positional arguments.- Parameters:
*other¶ –
one or more elements with which to create a UNION.
Changed in version 1.4.28: multiple elements are now accepted.
**kwargs¶ – keyword arguments are forwarded to the constructor for the newly created
CompoundSelectobject.
-
method
sqlalchemy.sql.expression.Select.execute(*multiparams, **params)¶ inherited from the
Executable.execute()method ofExecutableCompile and execute this
Executable.Deprecated since version 1.4: The
Executable.execute()method is considered legacy as of the 1.x series of SQLAlchemy and will be removed in 2.0. All statement execution in SQLAlchemy 2.0 is performed by theConnection.execute()method ofConnection, or in the ORM by theSession.execute()method ofSession. (Background on SQLAlchemy 2.0 at: Migrating to SQLAlchemy 2.0)
-
method
sqlalchemy.sql.expression.Select.execution_options(**kw)¶ inherited from the
Executable.execution_options()method ofExecutableSet non-SQL options for the statement which take effect during execution.
Execution options can be set on a per-statement or per
Connectionbasis. Additionally, theEngineand ORMQueryobjects provide access to execution options which they in turn configure upon connections.The
execution_options()method is generative. A new instance of this statement is returned that contains the options:statement = select(table.c.x, table.c.y) statement = statement.execution_options(autocommit=True)
Note that only a subset of possible execution options can be applied to a statement - these include “autocommit” and “stream_results”, but not “isolation_level” or “compiled_cache”. See
Connection.execution_options()for a full list of possible options.
-
method
sqlalchemy.sql.expression.Select.exists()¶ inherited from the
SelectBase.exists()method ofSelectBaseReturn an
Existsrepresentation of this selectable, which can be used as a column expression.The returned object is an instance of
Exists.New in version 1.4.
-
attribute
sqlalchemy.sql.expression.Select.exported_columns¶ inherited from the
SelectBase.exported_columnsattribute ofSelectBaseA
ColumnCollectionthat represents the “exported” columns of thisSelectable, not includingTextClauseconstructs.The “exported” columns for a
SelectBaseobject are synonymous with theSelectBase.selected_columnscollection.New in version 1.4.
-
method
sqlalchemy.sql.expression.Select.fetch(count, with_ties=False, percent=False)¶ inherited from the
GenerativeSelect.fetch()method ofGenerativeSelectReturn a new selectable with the given FETCH FIRST criterion applied.
This is a numeric value which usually renders as
FETCH {FIRST | NEXT} [ count ] {ROW | ROWS} {ONLY | WITH TIES}expression in the resulting select. This functionality is is currently implemented for Oracle, PostgreSQL, MSSQL.Use
GenerativeSelect.offset()to specify the offset.Note
The
GenerativeSelect.fetch()method will replace any clause applied withGenerativeSelect.limit().New in version 1.4.
- Parameters:
count¶ – an integer COUNT parameter, or a SQL expression that provides an integer result. When
percent=Truethis will represent the percentage of rows to return, not the absolute value. PassNoneto reset it.with_ties¶ – When
True, the WITH TIES option is used to return any additional rows that tie for the last place in the result set according to theORDER BYclause. TheORDER BYmay be mandatory in this case. Defaults toFalsepercent¶ – When
True,countrepresents the percentage of the total number of selected rows to return. Defaults toFalse
-
method
sqlalchemy.sql.expression.Select.filter(*criteria)¶ A synonym for the
Select.where()method.
-
method
sqlalchemy.sql.expression.Select.filter_by(**kwargs)¶ apply the given filtering criterion as a WHERE clause to this select.
-
method
sqlalchemy.sql.expression.Select.from_statement(statement)¶ Apply the columns which this
Selectwould select onto another statement.This operation is plugin-specific and will raise a not supported exception if this
Selectdoes not select from plugin-enabled entities.The statement is typically either a
text()orselect()construct, and should return the set of columns appropriate to the entities represented by thisSelect.See also
Getting ORM Results from Textual and Core Statements - usage examples in the ORM Querying Guide
-
attribute
sqlalchemy.sql.expression.Select.froms¶ Return the displayed list of
FromClauseelements.Deprecated since version 1.4.23: The
Select.fromsattribute is moved to theSelect.get_final_froms()method.
-
method
sqlalchemy.sql.expression.Select.get_children(**kwargs)¶ Return immediate child
Traversibleelements of thisTraversible.This is used for visit traversal.
**kw may contain flags that change the collection that is returned, for example to return a subset of items in order to cut down on larger traversals, or to return child items from a different context (such as schema-level collections instead of clause-level).
-
method
sqlalchemy.sql.expression.Select.get_execution_options()¶ inherited from the
Executable.get_execution_options()method ofExecutableGet the non-SQL options which will take effect during execution.
New in version 1.3.
See also
-
method
sqlalchemy.sql.expression.Select.get_final_froms()¶ Compute the final displayed list of
FromClauseelements.This method will run through the full computation required to determine what FROM elements will be displayed in the resulting SELECT statement, including shadowing individual tables with JOIN objects, as well as full computation for ORM use cases including eager loading clauses.
For ORM use, this accessor returns the post compilation list of FROM objects; this collection will include elements such as eagerly loaded tables and joins. The objects will not be ORM enabled and not work as a replacement for the
Select.select_froms()collection; additionally, the method is not well performing for an ORM enabled statement as it will incur the full ORM construction process.To retrieve the FROM list that’s implied by the “columns” collection passed to the
Selectoriginally, use theSelect.columns_clause_fromsaccessor.To select from an alternative set of columns while maintaining the FROM list, use the
Select.with_only_columns()method and pass theSelect.with_only_columns.maintain_column_fromsparameter.New in version 1.4.23: - the
Select.get_final_froms()method replaces the previousSelect.fromsaccessor, which is deprecated.See also
-
method
sqlalchemy.sql.expression.Select.get_label_style()¶ inherited from the
GenerativeSelect.get_label_style()method ofGenerativeSelectRetrieve the current label style.
New in version 1.4.
-
method
sqlalchemy.sql.expression.Select.group_by(*clauses)¶ inherited from the
GenerativeSelect.group_by()method ofGenerativeSelectReturn a new selectable with the given list of GROUP BY criterion applied.
All existing GROUP BY settings can be suppressed by passing
None.e.g.:
stmt = select(table.c.name, func.max(table.c.stat)).\ group_by(table.c.name)
- Parameters:
*clauses¶ – a series of
ColumnElementconstructs which will be used to generate an GROUP BY clause.
-
method
sqlalchemy.sql.expression.Select.having(having)¶ Return a new
select()construct with the given expression added to its HAVING clause, joined to the existing clause via AND, if any.
-
attribute
sqlalchemy.sql.expression.Select.inner_columns¶ An iterator of all
ColumnElementexpressions which would be rendered into the columns clause of the resulting SELECT statement.This method is legacy as of 1.4 and is superseded by the
Select.exported_columnscollection.
-
method
sqlalchemy.sql.expression.Select.intersect(*other, **kwargs)¶ Return a SQL
INTERSECTof this select() construct against the given selectables provided as positional arguments.- Parameters:
*other¶ –
one or more elements with which to create a UNION.
Changed in version 1.4.28: multiple elements are now accepted.
**kwargs¶ – keyword arguments are forwarded to the constructor for the newly created
CompoundSelectobject.
-
method
sqlalchemy.sql.expression.Select.intersect_all(*other, **kwargs)¶ Return a SQL
INTERSECT ALLof this select() construct against the given selectables provided as positional arguments.- Parameters:
*other¶ –
one or more elements with which to create a UNION.
Changed in version 1.4.28: multiple elements are now accepted.
**kwargs¶ – keyword arguments are forwarded to the constructor for the newly created
CompoundSelectobject.
-
method
sqlalchemy.sql.expression.Select.join(target, onclause=None, isouter=False, full=False)¶ Create a SQL JOIN against this
Selectobject’s criterion and apply generatively, returning the newly resultingSelect.E.g.:
stmt = select(user_table).join(address_table, user_table.c.id == address_table.c.user_id)
The above statement generates SQL similar to:
SELECT user.id, user.name FROM user JOIN address ON user.id = address.user_id
Changed in version 1.4:
Select.join()now creates aJoinobject between aFromClausesource that is within the FROM clause of the existing SELECT, and a given targetFromClause, and then adds thisJointo the FROM clause of the newly generated SELECT statement. This is completely reworked from the behavior in 1.3, which would instead create a subquery of the entireSelectand then join that subquery to the target.This is a backwards incompatible change as the previous behavior was mostly useless, producing an unnamed subquery rejected by most databases in any case. The new behavior is modeled after that of the very successful
Query.join()method in the ORM, in order to support the functionality ofQuerybeing available by using aSelectobject with anSession.See the notes for this change at select().join() and outerjoin() add JOIN criteria to the current query, rather than creating a subquery.
- Parameters:
target¶ – target table to join towards
onclause¶ – ON clause of the join. If omitted, an ON clause is generated automatically based on the
ForeignKeylinkages between the two tables, if one can be unambiguously determined, otherwise an error is raised.isouter¶ – if True, generate LEFT OUTER join. Same as
Select.outerjoin().full¶ – if True, generate FULL OUTER join.
See also
Explicit FROM clauses and JOINs - in the SQLAlchemy 1.4 / 2.0 Tutorial
Joins - in the ORM Querying Guide
-
method
sqlalchemy.sql.expression.Select.join_from(from_, target, onclause=None, isouter=False, full=False)¶ Create a SQL JOIN against this
Selectobject’s criterion and apply generatively, returning the newly resultingSelect.E.g.:
stmt = select(user_table, address_table).join_from( user_table, address_table, user_table.c.id == address_table.c.user_id )
The above statement generates SQL similar to:
SELECT user.id, user.name, address.id, address.email, address.user_id FROM user JOIN address ON user.id = address.user_id
New in version 1.4.
- Parameters:
from_¶ – the left side of the join, will be rendered in the FROM clause and is roughly equivalent to using the
Select.select_from()method.target¶ – target table to join towards
onclause¶ – ON clause of the join.
isouter¶ – if True, generate LEFT OUTER join. Same as
Select.outerjoin().full¶ – if True, generate FULL OUTER join.
See also
Explicit FROM clauses and JOINs - in the SQLAlchemy 1.4 / 2.0 Tutorial
Joins - in the ORM Querying Guide
-
method
sqlalchemy.sql.expression.Select.label(name)¶ inherited from the
SelectBase.label()method ofSelectBaseReturn a ‘scalar’ representation of this selectable, embedded as a subquery with a label.
See also
-
method
sqlalchemy.sql.expression.Select.lateral(name=None)¶ inherited from the
SelectBase.lateral()method ofSelectBaseReturn a LATERAL alias of this
Selectable.The return value is the
Lateralconstruct also provided by the top-levellateral()function.New in version 1.1.
See also
LATERAL correlation - overview of usage.
-
method
sqlalchemy.sql.expression.Select.limit(limit)¶ inherited from the
GenerativeSelect.limit()method ofGenerativeSelectReturn a new selectable with the given LIMIT criterion applied.
This is a numerical value which usually renders as a
LIMITexpression in the resulting select. Backends that don’t supportLIMITwill attempt to provide similar functionality.Note
The
GenerativeSelect.limit()method will replace any clause applied withGenerativeSelect.fetch().Changed in version 1.0.0: -
Select.limit()can now accept arbitrary SQL expressions as well as integer values.- Parameters:
limit¶ – an integer LIMIT parameter, or a SQL expression that provides an integer result. Pass
Noneto reset it.
-
method
sqlalchemy.sql.expression.Select.offset(offset)¶ inherited from the
GenerativeSelect.offset()method ofGenerativeSelectReturn a new selectable with the given OFFSET criterion applied.
This is a numeric value which usually renders as an
OFFSETexpression in the resulting select. Backends that don’t supportOFFSETwill attempt to provide similar functionality.Changed in version 1.0.0: -
Select.offset()can now accept arbitrary SQL expressions as well as integer values.- Parameters:
offset¶ – an integer OFFSET parameter, or a SQL expression that provides an integer result. Pass
Noneto reset it.
-
method
sqlalchemy.sql.expression.Select.options(*options)¶ inherited from the
Executable.options()method ofExecutableApply options to this statement.
In the general sense, options are any kind of Python object that can be interpreted by the SQL compiler for the statement. These options can be consumed by specific dialects or specific kinds of compilers.
The most commonly known kind of option are the ORM level options that apply “eager load” and other loading behaviors to an ORM query. However, options can theoretically be used for many other purposes.
For background on specific kinds of options for specific kinds of statements, refer to the documentation for those option objects.
Changed in version 1.4: - added
Generative.options()to Core statement objects towards the goal of allowing unified Core / ORM querying capabilities.See also
Deferred Column Loader Query Options - refers to options specific to the usage of ORM queries
Relationship Loading with Loader Options - refers to options specific to the usage of ORM queries
-
method
sqlalchemy.sql.expression.Select.order_by(*clauses)¶ inherited from the
GenerativeSelect.order_by()method ofGenerativeSelectReturn a new selectable with the given list of ORDER BY criteria applied.
e.g.:
stmt = select(table).order_by(table.c.id, table.c.name)
Calling this method multiple times is equivalent to calling it once with all the clauses concatenated. All existing ORDER BY criteria may be cancelled by passing
Noneby itself. New ORDER BY criteria may then be added by invokingQuery.order_by()again, e.g.:# will erase all ORDER BY and ORDER BY new_col alone stmt = stmt.order_by(None).order_by(new_col)
- Parameters:
*clauses¶ – a series of
ColumnElementconstructs which will be used to generate an ORDER BY clause.
See also
ORDER BY - in the SQLAlchemy 1.4 / 2.0 Tutorial
Ordering or Grouping by a Label - in the SQLAlchemy 1.4 / 2.0 Tutorial
-
method
sqlalchemy.sql.expression.Select.outerjoin(target, onclause=None, full=False)¶ Create a left outer join.
Parameters are the same as that of
Select.join().Changed in version 1.4:
Select.outerjoin()now creates aJoinobject between aFromClausesource that is within the FROM clause of the existing SELECT, and a given targetFromClause, and then adds thisJointo the FROM clause of the newly generated SELECT statement. This is completely reworked from the behavior in 1.3, which would instead create a subquery of the entireSelectand then join that subquery to the target.This is a backwards incompatible change as the previous behavior was mostly useless, producing an unnamed subquery rejected by most databases in any case. The new behavior is modeled after that of the very successful
Query.join()method in the ORM, in order to support the functionality ofQuerybeing available by using aSelectobject with anSession.See the notes for this change at select().join() and outerjoin() add JOIN criteria to the current query, rather than creating a subquery.
See also
Explicit FROM clauses and JOINs - in the SQLAlchemy 1.4 / 2.0 Tutorial
Joins - in the ORM Querying Guide
-
method
sqlalchemy.sql.expression.Select.outerjoin_from(from_, target, onclause=None, full=False)¶ Create a SQL LEFT OUTER JOIN against this
Selectobject’s criterion and apply generatively, returning the newly resultingSelect.Usage is the same as that of
Select.join_from().
-
method
sqlalchemy.sql.expression.Select.prefix_with(*expr, **kw)¶ inherited from the
HasPrefixes.prefix_with()method ofHasPrefixesAdd one or more expressions following the statement keyword, i.e. SELECT, INSERT, UPDATE, or DELETE. Generative.
This is used to support backend-specific prefix keywords such as those provided by MySQL.
E.g.:
stmt = table.insert().prefix_with("LOW_PRIORITY", dialect="mysql") # MySQL 5.7 optimizer hints stmt = select(table).prefix_with( "/*+ BKA(t1) */", dialect="mysql")
Multiple prefixes can be specified by multiple calls to
HasPrefixes.prefix_with().- Parameters:
*expr¶ – textual or
ClauseElementconstruct which will be rendered following the INSERT, UPDATE, or DELETE keyword.**kw¶ – A single keyword ‘dialect’ is accepted. This is an optional string dialect name which will limit rendering of this prefix to only that dialect.
-
method
sqlalchemy.sql.expression.Select.reduce_columns(only_synonyms=True)¶ Return a new
select()construct with redundantly named, equivalently-valued columns removed from the columns clause.“Redundant” here means two columns where one refers to the other either based on foreign key, or via a simple equality comparison in the WHERE clause of the statement. The primary purpose of this method is to automatically construct a select statement with all uniquely-named columns, without the need to use table-qualified labels as
Select.set_label_style()does.When columns are omitted based on foreign key, the referred-to column is the one that’s kept. When columns are omitted based on WHERE equivalence, the first column in the columns clause is the one that’s kept.
- Parameters:
only_synonyms¶ – when True, limit the removal of columns to those which have the same name as the equivalent. Otherwise, all columns that are equivalent to another are removed.
-
method
sqlalchemy.sql.expression.Select.replace_selectable(old, alias)¶ inherited from the
Selectable.replace_selectable()method ofSelectableReplace all occurrences of
FromClause‘old’ with the givenAliasobject, returning a copy of thisFromClause.Deprecated since version 1.4: The
Selectable.replace_selectable()method is deprecated, and will be removed in a future release. Similar functionality is available via the sqlalchemy.sql.visitors module.
-
method
sqlalchemy.sql.expression.Select.scalar(*multiparams, **params)¶ inherited from the
Executable.scalar()method ofExecutableCompile and execute this
Executable, returning the result’s scalar representation.Deprecated since version 1.4: The
Executable.scalar()method is considered legacy as of the 1.x series of SQLAlchemy and will be removed in 2.0. Scalar execution in SQLAlchemy 2.0 is performed by theConnection.scalar()method ofConnection, or in the ORM by theSession.scalar()method ofSession. (Background on SQLAlchemy 2.0 at: Migrating to SQLAlchemy 2.0)
-
method
sqlalchemy.sql.expression.Select.scalar_subquery()¶ inherited from the
SelectBase.scalar_subquery()method ofSelectBaseReturn a ‘scalar’ representation of this selectable, which can be used as a column expression.
The returned object is an instance of
ScalarSelect.Typically, a select statement which has only one column in its columns clause is eligible to be used as a scalar expression. The scalar subquery can then be used in the WHERE clause or columns clause of an enclosing SELECT.
Note that the scalar subquery differentiates from the FROM-level subquery that can be produced using the
SelectBase.subquery()method.See also
Scalar and Correlated Subqueries - in the 2.0 tutorial
-
method
sqlalchemy.sql.expression.Select.select(*arg, **kw)¶ inherited from the
SelectBase.select()method ofSelectBaseDeprecated since version 1.4: The
SelectBase.select()method is deprecated and will be removed in a future release; this method implicitly creates a subquery that should be explicit. Please callSelectBase.subquery()first in order to create a subquery, which then can be selected.
-
method
sqlalchemy.sql.expression.Select.select_from(*froms)¶ Return a new
select()construct with the given FROM expression(s) merged into its list of FROM objects.E.g.:
table1 = table('t1', column('a')) table2 = table('t2', column('b')) s = select(table1.c.a).\ select_from( table1.join(table2, table1.c.a==table2.c.b) )
The “from” list is a unique set on the identity of each element, so adding an already present
Tableor other selectable will have no effect. Passing aJointhat refers to an already presentTableor other selectable will have the effect of concealing the presence of that selectable as an individual element in the rendered FROM list, instead rendering it into a JOIN clause.While the typical purpose of
Select.select_from()is to replace the default, derived FROM clause with a join, it can also be called with individual table elements, multiple times if desired, in the case that the FROM clause cannot be fully derived from the columns clause:select(func.count('*')).select_from(table1)
-
attribute
sqlalchemy.sql.expression.Select.selected_columns¶ A
ColumnCollectionrepresenting the columns that this SELECT statement or similar construct returns in its result set, not includingTextClauseconstructs.This collection differs from the
FromClause.columnscollection of aFromClausein that the columns within this collection cannot be directly nested inside another SELECT statement; a subquery must be applied first which provides for the necessary parenthesization required by SQL.For a
select()construct, the collection here is exactly what would be rendered inside the “SELECT” statement, and theColumnElementobjects are directly present as they were given, e.g.:col1 = column('q', Integer) col2 = column('p', Integer) stmt = select(col1, col2)
Above,
stmt.selected_columnswould be a collection that contains thecol1andcol2objects directly. For a statement that is against aTableor otherFromClause, the collection will use theColumnElementobjects that are in theFromClause.ccollection of the from element.Note
The
Select.selected_columnscollection does not include expressions established in the columns clause using thetext()construct; these are silently omitted from the collection. To use plain textual column expressions inside of aSelectconstruct, use theliteral_column()construct.New in version 1.4.
-
method
sqlalchemy.sql.expression.Select.self_group(against=None)¶ Return a ‘grouping’ construct as per the
ClauseElementspecification.This produces an element that can be embedded in an expression. Note that this method is called automatically as needed when constructing expressions and should not require explicit use.
-
method
sqlalchemy.sql.expression.Select.set_label_style(style)¶ inherited from the
GenerativeSelect.set_label_style()method ofGenerativeSelectReturn a new selectable with the specified label style.
There are three “label styles” available,
LABEL_STYLE_DISAMBIGUATE_ONLY,LABEL_STYLE_TABLENAME_PLUS_COL, andLABEL_STYLE_NONE. The default style isLABEL_STYLE_DISAMBIGUATE_ONLY.In modern SQLAlchemy, there is not generally a need to change the labeling style, as per-expression labels are more effectively used by making use of the
ColumnElement.label()method. In past versions,LABEL_STYLE_TABLENAME_PLUS_COLwas used to disambiguate same-named columns from different tables, aliases, or subqueries; the newerLABEL_STYLE_DISAMBIGUATE_ONLYnow applies labels only to names that conflict with an existing name so that the impact of this labeling is minimal.The rationale for disambiguation is mostly so that all column expressions are available from a given
FromClause.ccollection when a subquery is created.New in version 1.4: - the
GenerativeSelect.set_label_style()method replaces the previous combination of.apply_labels(),.with_labels()anduse_labels=Truemethods and/or parameters.
-
method
sqlalchemy.sql.expression.Select.slice(start, stop)¶ inherited from the
GenerativeSelect.slice()method ofGenerativeSelectApply LIMIT / OFFSET to this statement based on a slice.
The start and stop indices behave like the argument to Python’s built-in
range()function. This method provides an alternative to usingLIMIT/OFFSETto get a slice of the query.For example,
stmt = select(User).order_by(User).id.slice(1, 3)
renders as
SELECT users.id AS users_id, users.name AS users_name FROM users ORDER BY users.id LIMIT ? OFFSET ? (2, 1)
Note
The
GenerativeSelect.slice()method will replace any clause applied withGenerativeSelect.fetch().New in version 1.4: Added the
GenerativeSelect.slice()method generalized from the ORM.
-
method
sqlalchemy.sql.expression.Select.subquery(name=None)¶ inherited from the
SelectBase.subquery()method ofSelectBaseReturn a subquery of this
SelectBase.A subquery is from a SQL perspective a parenthesized, named construct that can be placed in the FROM clause of another SELECT statement.
Given a SELECT statement such as:
stmt = select(table.c.id, table.c.name)
The above statement might look like:
SELECT table.id, table.name FROM table
The subquery form by itself renders the same way, however when embedded into the FROM clause of another SELECT statement, it becomes a named sub-element:
subq = stmt.subquery() new_stmt = select(subq)
The above renders as:
SELECT anon_1.id, anon_1.name FROM (SELECT table.id, table.name FROM table) AS anon_1
Historically,
SelectBase.subquery()is equivalent to calling theFromClause.alias()method on a FROM object; however, as aSelectBaseobject is not directly FROM object, theSelectBase.subquery()method provides clearer semantics.New in version 1.4.
-
method
sqlalchemy.sql.expression.Select.suffix_with(*expr, **kw)¶ inherited from the
HasSuffixes.suffix_with()method ofHasSuffixesAdd one or more expressions following the statement as a whole.
This is used to support backend-specific suffix keywords on certain constructs.
E.g.:
stmt = select(col1, col2).cte().suffix_with( "cycle empno set y_cycle to 1 default 0", dialect="oracle")
Multiple suffixes can be specified by multiple calls to
HasSuffixes.suffix_with().- Parameters:
*expr¶ – textual or
ClauseElementconstruct which will be rendered following the target clause.**kw¶ – A single keyword ‘dialect’ is accepted. This is an optional string dialect name which will limit rendering of this suffix to only that dialect.
-
method
sqlalchemy.sql.expression.Select.union(*other, **kwargs)¶ Return a SQL
UNIONof this select() construct against the given selectables provided as positional arguments.- Parameters:
*other¶ –
one or more elements with which to create a UNION.
Changed in version 1.4.28: multiple elements are now accepted.
**kwargs¶ – keyword arguments are forwarded to the constructor for the newly created
CompoundSelectobject.
-
method
sqlalchemy.sql.expression.Select.union_all(*other, **kwargs)¶ Return a SQL
UNION ALLof this select() construct against the given selectables provided as positional arguments.- Parameters:
*other¶ –
one or more elements with which to create a UNION.
Changed in version 1.4.28: multiple elements are now accepted.
**kwargs¶ – keyword arguments are forwarded to the constructor for the newly created
CompoundSelectobject.
-
method
sqlalchemy.sql.expression.Select.where(*whereclause)¶ Return a new
select()construct with the given expression added to its WHERE clause, joined to the existing clause via AND, if any.
-
attribute
sqlalchemy.sql.expression.Select.whereclause¶ Return the completed WHERE clause for this
Selectstatement.This assembles the current collection of WHERE criteria into a single
BooleanClauseListconstruct.New in version 1.4.
-
method
sqlalchemy.sql.expression.Select.with_for_update(nowait=False, read=False, of=None, skip_locked=False, key_share=False)¶ inherited from the
GenerativeSelect.with_for_update()method ofGenerativeSelectSpecify a
FOR UPDATEclause for thisGenerativeSelect.E.g.:
stmt = select(table).with_for_update(nowait=True)
On a database like PostgreSQL or Oracle, the above would render a statement like:
SELECT table.a, table.b FROM table FOR UPDATE NOWAIT
on other backends, the
nowaitoption is ignored and instead would produce:SELECT table.a, table.b FROM table FOR UPDATE
When called with no arguments, the statement will render with the suffix
FOR UPDATE. Additional arguments can then be provided which allow for common database-specific variants.- Parameters:
nowait¶ – boolean; will render
FOR UPDATE NOWAITon Oracle and PostgreSQL dialects.read¶ – boolean; will render
LOCK IN SHARE MODEon MySQL,FOR SHAREon PostgreSQL. On PostgreSQL, when combined withnowait, will renderFOR SHARE NOWAIT.of¶ – SQL expression or list of SQL expression elements (typically
Columnobjects or a compatible expression) which will render into aFOR UPDATE OFclause; supported by PostgreSQL and Oracle. May render as a table or as a column depending on backend.skip_locked¶ – boolean, will render
FOR UPDATE SKIP LOCKEDon Oracle and PostgreSQL dialects orFOR SHARE SKIP LOCKEDifread=Trueis also specified.key_share¶ – boolean, will render
FOR NO KEY UPDATE, or if combined withread=Truewill renderFOR KEY SHARE, on the PostgreSQL dialect.
-
method
sqlalchemy.sql.expression.Select.with_hint(selectable, text, dialect_name='*')¶ inherited from the
HasHints.with_hint()method ofHasHintsAdd an indexing or other executional context hint for the given selectable to this
Selector other selectable object.The text of the hint is rendered in the appropriate location for the database backend in use, relative to the given
TableorAliaspassed as theselectableargument. The dialect implementation typically uses Python string substitution syntax with the token%(name)sto render the name of the table or alias. E.g. when using Oracle, the following:select(mytable).\ with_hint(mytable, "index(%(name)s ix_mytable)")
Would render SQL as:
select /*+ index(mytable ix_mytable) */ ... from mytable
The
dialect_nameoption will limit the rendering of a particular hint to a particular backend. Such as, to add hints for both Oracle and Sybase simultaneously:select(mytable).\ with_hint(mytable, "index(%(name)s ix_mytable)", 'oracle').\ with_hint(mytable, "WITH INDEX ix_mytable", 'sybase')
See also
-
method
sqlalchemy.sql.expression.Select.with_only_columns(*columns, **kw)¶ Return a new
select()construct with its columns clause replaced with the given columns.By default, this method is exactly equivalent to as if the original
select()had been called with the given columns clause. E.g. a statement:s = select(table1.c.a, table1.c.b) s = s.with_only_columns(table1.c.b)
should be exactly equivalent to:
s = select(table1.c.b)
In this mode of operation,
Select.with_only_columns()will also dynamically alter the FROM clause of the statement if it is not explicitly stated. To maintain the existing set of FROMs including those implied by the current columns clause, add theSelect.with_only_columns.maintain_column_fromsparameter:s = select(table1.c.a, table2.c.b) s = s.with_only_columns(table1.c.a, maintain_column_froms=True)
The above parameter performs a transfer of the effective FROMs in the columns collection to the
Select.select_from()method, as though the following were invoked:s = select(table1.c.a, table2.c.b) s = s.select_from(table1, table2).with_only_columns(table1.c.a)
The
Select.with_only_columns.maintain_column_fromsparameter makes use of theSelect.columns_clause_fromscollection and performs an operation equivalent to the following:s = select(table1.c.a, table2.c.b) s = s.select_from(*s.columns_clause_froms).with_only_columns(table1.c.a)
- Parameters:
*columns¶ –
column expressions to be used.
Changed in version 1.4: the
Select.with_only_columns()method accepts the list of column expressions positionally; passing the expressions as a list is deprecated.maintain_column_froms¶ –
boolean parameter that will ensure the FROM list implied from the current columns clause will be transferred to the
Select.select_from()method first.New in version 1.4.23.
-
method
sqlalchemy.sql.expression.Select.with_statement_hint(text, dialect_name='*')¶ inherited from the
HasHints.with_statement_hint()method ofHasHintsAdd a statement hint to this
Selector other selectable object.This method is similar to
Select.with_hint()except that it does not require an individual table, and instead applies to the statement as a whole.Hints here are specific to the backend database and may include directives such as isolation levels, file directives, fetch directives, etc.
New in version 1.0.0.
See also
Select.prefix_with()- generic SELECT prefixing which also can suit some database-specific HINT syntaxes such as MySQL optimizer hints
-
method
- class sqlalchemy.sql.expression.Selectable¶
Mark a class as being selectable.
Class signature
class
sqlalchemy.sql.expression.Selectable(sqlalchemy.sql.expression.ReturnsRows)-
method
sqlalchemy.sql.expression.Selectable.corresponding_column(column, require_embedded=False)¶ Given a
ColumnElement, return the exportedColumnElementobject from theSelectable.exported_columnscollection of thisSelectablewhich corresponds to that originalColumnElementvia a common ancestor column.- Parameters:
column¶ – the target
ColumnElementto be matched.require_embedded¶ – only return corresponding columns for the given
ColumnElement, if the givenColumnElementis actually present within a sub-element of thisSelectable. Normally the column will match if it merely shares a common ancestor with one of the exported columns of thisSelectable.
See also
Selectable.exported_columns- theColumnCollectionthat is used for the operation.ColumnCollection.corresponding_column()- implementation method.
-
attribute
sqlalchemy.sql.expression.Selectable.exported_columns¶ inherited from the
ReturnsRows.exported_columnsattribute ofReturnsRowsA
ColumnCollectionthat represents the “exported” columns of thisReturnsRows.The “exported” columns represent the collection of
ColumnElementexpressions that are rendered by this SQL construct. There are primary varieties which are the “FROM clause columns” of a FROM clause, such as a table, join, or subquery, the “SELECTed columns”, which are the columns in the “columns clause” of a SELECT statement, and the RETURNING columns in a DML statement..New in version 1.4.
-
method
sqlalchemy.sql.expression.Selectable.lateral(name=None)¶ Return a LATERAL alias of this
Selectable.The return value is the
Lateralconstruct also provided by the top-levellateral()function.New in version 1.1.
See also
LATERAL correlation - overview of usage.
-
method
sqlalchemy.sql.expression.Selectable.replace_selectable(old, alias)¶ Replace all occurrences of
FromClause‘old’ with the givenAliasobject, returning a copy of thisFromClause.Deprecated since version 1.4: The
Selectable.replace_selectable()method is deprecated, and will be removed in a future release. Similar functionality is available via the sqlalchemy.sql.visitors module.
-
method
- class sqlalchemy.sql.expression.SelectBase¶
Base class for SELECT statements.
This includes
Select,CompoundSelectandTextualSelect.Members
add_cte(), alias(), as_scalar(), bind, c, corresponding_column(), cte(), execute(), execution_options(), exists(), exported_columns, get_execution_options(), label(), lateral(), options(), replace_selectable(), scalar(), scalar_subquery(), select(), selected_columns, subquery()
Class signature
class
sqlalchemy.sql.expression.SelectBase(sqlalchemy.sql.roles.SelectStatementRole,sqlalchemy.sql.roles.DMLSelectRole,sqlalchemy.sql.roles.CompoundElementRole,sqlalchemy.sql.roles.InElementRole,sqlalchemy.sql.expression.HasCTE,sqlalchemy.sql.expression.Executable,sqlalchemy.sql.annotation.SupportsCloneAnnotations,sqlalchemy.sql.expression.Selectable)-
method
sqlalchemy.sql.expression.SelectBase.add_cte(cte)¶ inherited from the
HasCTE.add_cte()method ofHasCTEAdd a
CTEto this statement object that will be independently rendered even if not referenced in the statement otherwise.This feature is useful for the use case of embedding a DML statement such as an INSERT or UPDATE as a CTE inline with a primary statement that may draw from its results indirectly; while PostgreSQL is known to support this usage, it may not be supported by other backends.
E.g.:
from sqlalchemy import table, column, select t = table('t', column('c1'), column('c2')) ins = t.insert().values({"c1": "x", "c2": "y"}).cte() stmt = select(t).add_cte(ins)
Would render:
WITH anon_1 AS (INSERT INTO t (c1, c2) VALUES (:param_1, :param_2)) SELECT t.c1, t.c2 FROM t
Above, the “anon_1” CTE is not referred towards in the SELECT statement, however still accomplishes the task of running an INSERT statement.
Similarly in a DML-related context, using the PostgreSQL
Insertconstruct to generate an “upsert”:from sqlalchemy import table, column from sqlalchemy.dialects.postgresql import insert t = table("t", column("c1"), column("c2")) delete_statement_cte = ( t.delete().where(t.c.c1 < 1).cte("deletions") ) insert_stmt = insert(t).values({"c1": 1, "c2": 2}) update_statement = insert_stmt.on_conflict_do_update( index_elements=[t.c.c1], set_={ "c1": insert_stmt.excluded.c1, "c2": insert_stmt.excluded.c2, }, ).add_cte(delete_statement_cte) print(update_statement)
The above statement renders as:
WITH deletions AS (DELETE FROM t WHERE t.c1 < %(c1_1)s) INSERT INTO t (c1, c2) VALUES (%(c1)s, %(c2)s) ON CONFLICT (c1) DO UPDATE SET c1 = excluded.c1, c2 = excluded.c2
New in version 1.4.21.
-
method
sqlalchemy.sql.expression.SelectBase.alias(name=None, flat=False)¶ Return a named subquery against this
SelectBase.For a
SelectBase(as opposed to aFromClause), this returns aSubqueryobject which behaves mostly the same as theAliasobject that is used with aFromClause.Changed in version 1.4: The
SelectBase.alias()method is now a synonym for theSelectBase.subquery()method.
-
method
sqlalchemy.sql.expression.SelectBase.as_scalar()¶ Deprecated since version 1.4: The
SelectBase.as_scalar()method is deprecated and will be removed in a future release. Please refer toSelectBase.scalar_subquery().
-
attribute
sqlalchemy.sql.expression.SelectBase.bind¶ inherited from the
Executable.bindattribute ofExecutableReturns the
EngineorConnectionto which thisExecutableis bound, or None if none found.Deprecated since version 1.4: The
Executable.bindattribute is considered legacy as of the 1.x series of SQLAlchemy and will be removed in 2.0. Bound metadata is being removed as of SQLAlchemy 2.0. (Background on SQLAlchemy 2.0 at: Migrating to SQLAlchemy 2.0)This is a traversal which checks locally, then checks among the “from” clauses of associated objects until a bound engine or connection is found.
-
attribute
sqlalchemy.sql.expression.SelectBase.c¶ Deprecated since version 1.4: The
SelectBase.candSelectBase.columnsattributes are deprecated and will be removed in a future release; these attributes implicitly create a subquery that should be explicit. Please callSelectBase.subquery()first in order to create a subquery, which then contains this attribute. To access the columns that this SELECT object SELECTs from, use theSelectBase.selected_columnsattribute.
-
method
sqlalchemy.sql.expression.SelectBase.corresponding_column(column, require_embedded=False)¶ inherited from the
Selectable.corresponding_column()method ofSelectableGiven a
ColumnElement, return the exportedColumnElementobject from theSelectable.exported_columnscollection of thisSelectablewhich corresponds to that originalColumnElementvia a common ancestor column.- Parameters:
column¶ – the target
ColumnElementto be matched.require_embedded¶ – only return corresponding columns for the given
ColumnElement, if the givenColumnElementis actually present within a sub-element of thisSelectable. Normally the column will match if it merely shares a common ancestor with one of the exported columns of thisSelectable.
See also
Selectable.exported_columns- theColumnCollectionthat is used for the operation.ColumnCollection.corresponding_column()- implementation method.
-
method
sqlalchemy.sql.expression.SelectBase.cte(name=None, recursive=False, nesting=False)¶ inherited from the
HasCTE.cte()method ofHasCTEReturn a new
CTE, or Common Table Expression instance.Common table expressions are a SQL standard whereby SELECT statements can draw upon secondary statements specified along with the primary statement, using a clause called “WITH”. Special semantics regarding UNION can also be employed to allow “recursive” queries, where a SELECT statement can draw upon the set of rows that have previously been selected.
CTEs can also be applied to DML constructs UPDATE, INSERT and DELETE on some databases, both as a source of CTE rows when combined with RETURNING, as well as a consumer of CTE rows.
Changed in version 1.1: Added support for UPDATE/INSERT/DELETE as CTE, CTEs added to UPDATE/INSERT/DELETE.
SQLAlchemy detects
CTEobjects, which are treated similarly toAliasobjects, as special elements to be delivered to the FROM clause of the statement as well as to a WITH clause at the top of the statement.For special prefixes such as PostgreSQL “MATERIALIZED” and “NOT MATERIALIZED”, the
CTE.prefix_with()method may be used to establish these.Changed in version 1.3.13: Added support for prefixes. In particular - MATERIALIZED and NOT MATERIALIZED.
- Parameters:
name¶ – name given to the common table expression. Like
FromClause.alias(), the name can be left asNonein which case an anonymous symbol will be used at query compile time.recursive¶ – if
True, will renderWITH RECURSIVE. A recursive common table expression is intended to be used in conjunction with UNION ALL in order to derive rows from those already selected.nesting¶ –
if
True, will render the CTE locally to the actual statement.New in version 1.4.24.
The following examples include two from PostgreSQL’s documentation at https://www.postgresql.org/docs/current/static/queries-with.html, as well as additional examples.
Example 1, non recursive:
from sqlalchemy import (Table, Column, String, Integer, MetaData, select, func) metadata = MetaData() orders = Table('orders', metadata, Column('region', String), Column('amount', Integer), Column('product', String), Column('quantity', Integer) ) regional_sales = select( orders.c.region, func.sum(orders.c.amount).label('total_sales') ).group_by(orders.c.region).cte("regional_sales") top_regions = select(regional_sales.c.region).\ where( regional_sales.c.total_sales > select( func.sum(regional_sales.c.total_sales) / 10 ) ).cte("top_regions") statement = select( orders.c.region, orders.c.product, func.sum(orders.c.quantity).label("product_units"), func.sum(orders.c.amount).label("product_sales") ).where(orders.c.region.in_( select(top_regions.c.region) )).group_by(orders.c.region, orders.c.product) result = conn.execute(statement).fetchall()
Example 2, WITH RECURSIVE:
from sqlalchemy import (Table, Column, String, Integer, MetaData, select, func) metadata = MetaData() parts = Table('parts', metadata, Column('part', String), Column('sub_part', String), Column('quantity', Integer), ) included_parts = select(\ parts.c.sub_part, parts.c.part, parts.c.quantity\ ).\ where(parts.c.part=='our part').\ cte(recursive=True) incl_alias = included_parts.alias() parts_alias = parts.alias() included_parts = included_parts.union_all( select( parts_alias.c.sub_part, parts_alias.c.part, parts_alias.c.quantity ).\ where(parts_alias.c.part==incl_alias.c.sub_part) ) statement = select( included_parts.c.sub_part, func.sum(included_parts.c.quantity). label('total_quantity') ).\ group_by(included_parts.c.sub_part) result = conn.execute(statement).fetchall()
Example 3, an upsert using UPDATE and INSERT with CTEs:
from datetime import date from sqlalchemy import (MetaData, Table, Column, Integer, Date, select, literal, and_, exists) metadata = MetaData() visitors = Table('visitors', metadata, Column('product_id', Integer, primary_key=True), Column('date', Date, primary_key=True), Column('count', Integer), ) # add 5 visitors for the product_id == 1 product_id = 1 day = date.today() count = 5 update_cte = ( visitors.update() .where(and_(visitors.c.product_id == product_id, visitors.c.date == day)) .values(count=visitors.c.count + count) .returning(literal(1)) .cte('update_cte') ) upsert = visitors.insert().from_select( [visitors.c.product_id, visitors.c.date, visitors.c.count], select(literal(product_id), literal(day), literal(count)) .where(~exists(update_cte.select())) ) connection.execute(upsert)
Example 4, Nesting CTE (SQLAlchemy 1.4.24 and above):
value_a = select( literal("root").label("n") ).cte("value_a") # A nested CTE with the same name as the root one value_a_nested = select( literal("nesting").label("n") ).cte("value_a", nesting=True) # Nesting CTEs takes ascendency locally # over the CTEs at a higher level value_b = select(value_a_nested.c.n).cte("value_b") value_ab = select(value_a.c.n.label("a"), value_b.c.n.label("b"))
The above query will render the second CTE nested inside the first, shown with inline parameters below as:
WITH value_a AS (SELECT 'root' AS n), value_b AS (WITH value_a AS (SELECT 'nesting' AS n) SELECT value_a.n AS n FROM value_a) SELECT value_a.n AS a, value_b.n AS b FROM value_a, value_b
Example 5, Non-Linear CTE (SQLAlchemy 1.4.28 and above):
edge = Table( "edge", metadata, Column("id", Integer, primary_key=True), Column("left", Integer), Column("right", Integer), ) root_node = select(literal(1).label("node")).cte( "nodes", recursive=True ) left_edge = select(edge.c.left).join( root_node, edge.c.right == root_node.c.node ) right_edge = select(edge.c.right).join( root_node, edge.c.left == root_node.c.node ) subgraph_cte = root_node.union(left_edge, right_edge) subgraph = select(subgraph_cte)
The above query will render 2 UNIONs inside the recursive CTE:
WITH RECURSIVE nodes(node) AS ( SELECT 1 AS node UNION SELECT edge."left" AS "left" FROM edge JOIN nodes ON edge."right" = nodes.node UNION SELECT edge."right" AS "right" FROM edge JOIN nodes ON edge."left" = nodes.node ) SELECT nodes.node FROM nodes
See also
Query.cte()- ORM version ofHasCTE.cte().
-
method
sqlalchemy.sql.expression.SelectBase.execute(*multiparams, **params)¶ inherited from the
Executable.execute()method ofExecutableCompile and execute this
Executable.Deprecated since version 1.4: The
Executable.execute()method is considered legacy as of the 1.x series of SQLAlchemy and will be removed in 2.0. All statement execution in SQLAlchemy 2.0 is performed by theConnection.execute()method ofConnection, or in the ORM by theSession.execute()method ofSession. (Background on SQLAlchemy 2.0 at: Migrating to SQLAlchemy 2.0)
-
method
sqlalchemy.sql.expression.SelectBase.execution_options(**kw)¶ inherited from the
Executable.execution_options()method ofExecutableSet non-SQL options for the statement which take effect during execution.
Execution options can be set on a per-statement or per
Connectionbasis. Additionally, theEngineand ORMQueryobjects provide access to execution options which they in turn configure upon connections.The
execution_options()method is generative. A new instance of this statement is returned that contains the options:statement = select(table.c.x, table.c.y) statement = statement.execution_options(autocommit=True)
Note that only a subset of possible execution options can be applied to a statement - these include “autocommit” and “stream_results”, but not “isolation_level” or “compiled_cache”. See
Connection.execution_options()for a full list of possible options.
-
method
sqlalchemy.sql.expression.SelectBase.exists()¶ Return an
Existsrepresentation of this selectable, which can be used as a column expression.The returned object is an instance of
Exists.New in version 1.4.
-
attribute
sqlalchemy.sql.expression.SelectBase.exported_columns¶ A
ColumnCollectionthat represents the “exported” columns of thisSelectable, not includingTextClauseconstructs.The “exported” columns for a
SelectBaseobject are synonymous with theSelectBase.selected_columnscollection.New in version 1.4.
-
method
sqlalchemy.sql.expression.SelectBase.get_execution_options()¶ inherited from the
Executable.get_execution_options()method ofExecutableGet the non-SQL options which will take effect during execution.
New in version 1.3.
See also
-
method
sqlalchemy.sql.expression.SelectBase.label(name)¶ Return a ‘scalar’ representation of this selectable, embedded as a subquery with a label.
See also
-
method
sqlalchemy.sql.expression.SelectBase.lateral(name=None)¶ Return a LATERAL alias of this
Selectable.The return value is the
Lateralconstruct also provided by the top-levellateral()function.New in version 1.1.
See also
LATERAL correlation - overview of usage.
-
method
sqlalchemy.sql.expression.SelectBase.options(*options)¶ inherited from the
Executable.options()method ofExecutableApply options to this statement.
In the general sense, options are any kind of Python object that can be interpreted by the SQL compiler for the statement. These options can be consumed by specific dialects or specific kinds of compilers.
The most commonly known kind of option are the ORM level options that apply “eager load” and other loading behaviors to an ORM query. However, options can theoretically be used for many other purposes.
For background on specific kinds of options for specific kinds of statements, refer to the documentation for those option objects.
Changed in version 1.4: - added
Generative.options()to Core statement objects towards the goal of allowing unified Core / ORM querying capabilities.See also
Deferred Column Loader Query Options - refers to options specific to the usage of ORM queries
Relationship Loading with Loader Options - refers to options specific to the usage of ORM queries
-
method
sqlalchemy.sql.expression.SelectBase.replace_selectable(old, alias)¶ inherited from the
Selectable.replace_selectable()method ofSelectableReplace all occurrences of
FromClause‘old’ with the givenAliasobject, returning a copy of thisFromClause.Deprecated since version 1.4: The
Selectable.replace_selectable()method is deprecated, and will be removed in a future release. Similar functionality is available via the sqlalchemy.sql.visitors module.
-
method
sqlalchemy.sql.expression.SelectBase.scalar(*multiparams, **params)¶ inherited from the
Executable.scalar()method ofExecutableCompile and execute this
Executable, returning the result’s scalar representation.Deprecated since version 1.4: The
Executable.scalar()method is considered legacy as of the 1.x series of SQLAlchemy and will be removed in 2.0. Scalar execution in SQLAlchemy 2.0 is performed by theConnection.scalar()method ofConnection, or in the ORM by theSession.scalar()method ofSession. (Background on SQLAlchemy 2.0 at: Migrating to SQLAlchemy 2.0)
-
method
sqlalchemy.sql.expression.SelectBase.scalar_subquery()¶ Return a ‘scalar’ representation of this selectable, which can be used as a column expression.
The returned object is an instance of
ScalarSelect.Typically, a select statement which has only one column in its columns clause is eligible to be used as a scalar expression. The scalar subquery can then be used in the WHERE clause or columns clause of an enclosing SELECT.
Note that the scalar subquery differentiates from the FROM-level subquery that can be produced using the
SelectBase.subquery()method.See also
Scalar and Correlated Subqueries - in the 2.0 tutorial
-
method
sqlalchemy.sql.expression.SelectBase.select(*arg, **kw)¶ Deprecated since version 1.4: The
SelectBase.select()method is deprecated and will be removed in a future release; this method implicitly creates a subquery that should be explicit. Please callSelectBase.subquery()first in order to create a subquery, which then can be selected.
-
attribute
sqlalchemy.sql.expression.SelectBase.selected_columns¶ A
ColumnCollectionrepresenting the columns that this SELECT statement or similar construct returns in its result set.This collection differs from the
FromClause.columnscollection of aFromClausein that the columns within this collection cannot be directly nested inside another SELECT statement; a subquery must be applied first which provides for the necessary parenthesization required by SQL.Note
The
SelectBase.selected_columnscollection does not include expressions established in the columns clause using thetext()construct; these are silently omitted from the collection. To use plain textual column expressions inside of aSelectconstruct, use theliteral_column()construct.See also
New in version 1.4.
-
method
sqlalchemy.sql.expression.SelectBase.subquery(name=None)¶ Return a subquery of this
SelectBase.A subquery is from a SQL perspective a parenthesized, named construct that can be placed in the FROM clause of another SELECT statement.
Given a SELECT statement such as:
stmt = select(table.c.id, table.c.name)
The above statement might look like:
SELECT table.id, table.name FROM table
The subquery form by itself renders the same way, however when embedded into the FROM clause of another SELECT statement, it becomes a named sub-element:
subq = stmt.subquery() new_stmt = select(subq)
The above renders as:
SELECT anon_1.id, anon_1.name FROM (SELECT table.id, table.name FROM table) AS anon_1
Historically,
SelectBase.subquery()is equivalent to calling theFromClause.alias()method on a FROM object; however, as aSelectBaseobject is not directly FROM object, theSelectBase.subquery()method provides clearer semantics.New in version 1.4.
-
method
- class sqlalchemy.sql.expression.Subquery(*arg, **kw)¶
Represent a subquery of a SELECT.
A
Subqueryis created by invoking theSelectBase.subquery()method, or for convenience theSelectBase.alias()method, on anySelectBasesubclass which includesSelect,CompoundSelect, andTextualSelect. As rendered in a FROM clause, it represents the body of the SELECT statement inside of parenthesis, followed by the usual “AS <somename>” that defines all “alias” objects.The
Subqueryobject is very similar to theAliasobject and can be used in an equivalent way. The difference betweenAliasandSubqueryis thatAliasalways contains aFromClauseobject whereasSubqueryalways contains aSelectBaseobject.New in version 1.4: The
Subqueryclass was added which now serves the purpose of providing an aliased version of a SELECT statement.Members
Class signature
class
sqlalchemy.sql.expression.Subquery(sqlalchemy.sql.expression.AliasedReturnsRows)-
method
sqlalchemy.sql.expression.Subquery.as_scalar()¶ Deprecated since version 1.4: The
Subquery.as_scalar()method, which was previouslyAlias.as_scalar()prior to version 1.4, is deprecated and will be removed in a future release; Please use theSelect.scalar_subquery()method of theselect()construct before constructing a subquery object, or with the ORM use theQuery.scalar_subquery()method.
-
attribute
sqlalchemy.sql.expression.Subquery.inherit_cache = True¶ Indicate if this
HasCacheKeyinstance should make use of the cache key generation scheme used by its immediate superclass.The attribute defaults to
None, which indicates that a construct has not yet taken into account whether or not its appropriate for it to participate in caching; this is functionally equivalent to setting the value toFalse, except that a warning is also emitted.This flag can be set to
Trueon a particular class, if the SQL that corresponds to the object does not change based on attributes which are local to this class, and not its superclass.See also
Enabling Caching Support for Custom Constructs - General guideslines for setting the
HasCacheKey.inherit_cacheattribute for third-party or user defined SQL constructs.
-
method
- class sqlalchemy.sql.expression.TableClause(name, *columns, **kw)¶
Represents a minimal “table” construct.
This is a lightweight table object that has only a name, a collection of columns, which are typically produced by the
column()function, and a schema:from sqlalchemy import table, column user = table("user", column("id"), column("name"), column("description"), )
The
TableClauseconstruct serves as the base for the more commonly usedTableobject, providing the usual set ofFromClauseservices including the.c.collection and statement generation methods.It does not provide all the additional schema-level services of
Table, including constraints, references to other tables, or support forMetaData-level services. It’s useful on its own as an ad-hoc construct used to generate quick SQL statements when a more fully fledgedTableis not on hand.Members
__init__(), alias(), c, columns, compare(), compile(), corresponding_column(), delete(), description, entity_namespace, exported_columns, foreign_keys, get_children(), implicit_returning, inherit_cache, insert(), is_derived_from(), join(), lateral(), memoized_instancemethod(), outerjoin(), params(), primary_key, replace_selectable(), schema, select(), self_group(), table_valued(), tablesample(), unique_params(), update()
Class signature
class
sqlalchemy.sql.expression.TableClause(sqlalchemy.sql.roles.DMLTableRole,sqlalchemy.sql.expression.Immutable,sqlalchemy.sql.expression.FromClause)-
method
sqlalchemy.sql.expression.TableClause.__init__(name, *columns, **kw)¶ Construct a new
TableClauseobject.This constructor is mirrored as a public API function; see
sqlalchemy.sql.expression.table()for a full usage and argument description.
-
method
sqlalchemy.sql.expression.TableClause.alias(name=None, flat=False)¶ inherited from the
FromClause.alias()method ofFromClauseReturn an alias of this
FromClause.E.g.:
a2 = some_table.alias('a2')
The above code creates an
Aliasobject which can be used as a FROM clause in any SELECT statement.
-
attribute
sqlalchemy.sql.expression.TableClause.c¶ inherited from the
FromClause.cattribute ofFromClauseA named-based collection of
ColumnElementobjects maintained by thisFromClause.The
FromClause.cattribute is an alias for theFromClause.columnsattribute.- Returns:
-
attribute
sqlalchemy.sql.expression.TableClause.columns¶ inherited from the
FromClause.columnsattribute ofFromClauseA named-based collection of
ColumnElementobjects maintained by thisFromClause.The
columns, orccollection, is the gateway to the construction of SQL expressions using table-bound or other selectable-bound columns:select(mytable).where(mytable.c.somecolumn == 5)
- Returns:
a
ColumnCollectionobject.
-
method
sqlalchemy.sql.expression.TableClause.compare(other, **kw)¶ inherited from the
ClauseElement.compare()method ofClauseElementCompare this
ClauseElementto the givenClauseElement.Subclasses should override the default behavior, which is a straight identity comparison.
**kw are arguments consumed by subclass
compare()methods and may be used to modify the criteria for comparison (seeColumnElement).
-
method
sqlalchemy.sql.expression.TableClause.compile(bind=None, dialect=None, **kw)¶ inherited from the
ClauseElement.compile()method ofClauseElementCompile this SQL expression.
The return value is a
Compiledobject. Callingstr()orunicode()on the returned value will yield a string representation of the result. TheCompiledobject also can return a dictionary of bind parameter names and values using theparamsaccessor.- Parameters:
bind¶ – An
EngineorConnectionfrom which aCompiledwill be acquired. This argument takes precedence over thisClauseElement’s bound engine, if any.column_keys¶ – Used for INSERT and UPDATE statements, a list of column names which should be present in the VALUES clause of the compiled statement. If
None, all columns from the target table object are rendered.dialect¶ – A
Dialectinstance from which aCompiledwill be acquired. This argument takes precedence over the bind argument as well as thisClauseElement‘s bound engine, if any.compile_kwargs¶ –
optional dictionary of additional parameters that will be passed through to the compiler within all “visit” methods. This allows any custom flag to be passed through to a custom compilation construct, for example. It is also used for the case of passing the
literal_bindsflag through:from sqlalchemy.sql import table, column, select t = table('t', column('x')) s = select(t).where(t.c.x == 5) print(s.compile(compile_kwargs={"literal_binds": True}))
New in version 0.9.0.
-
method
sqlalchemy.sql.expression.TableClause.corresponding_column(column, require_embedded=False)¶ inherited from the
Selectable.corresponding_column()method ofSelectableGiven a
ColumnElement, return the exportedColumnElementobject from theSelectable.exported_columnscollection of thisSelectablewhich corresponds to that originalColumnElementvia a common ancestor column.- Parameters:
column¶ – the target
ColumnElementto be matched.require_embedded¶ – only return corresponding columns for the given
ColumnElement, if the givenColumnElementis actually present within a sub-element of thisSelectable. Normally the column will match if it merely shares a common ancestor with one of the exported columns of thisSelectable.
See also
Selectable.exported_columns- theColumnCollectionthat is used for the operation.ColumnCollection.corresponding_column()- implementation method.
-
method
sqlalchemy.sql.expression.TableClause.delete(whereclause=None, **kwargs)¶ Generate a
delete()construct against thisTableClause.E.g.:
table.delete().where(table.c.id==7)
See
delete()for argument and usage information.
-
attribute
sqlalchemy.sql.expression.TableClause.description¶
-
attribute
sqlalchemy.sql.expression.TableClause.entity_namespace¶ inherited from the
FromClause.entity_namespaceattribute ofFromClauseReturn a namespace used for name-based access in SQL expressions.
This is the namespace that is used to resolve “filter_by()” type expressions, such as:
stmt.filter_by(address='some address')
It defaults to the
.ccollection, however internally it can be overridden using the “entity_namespace” annotation to deliver alternative results.
-
attribute
sqlalchemy.sql.expression.TableClause.exported_columns¶ inherited from the
FromClause.exported_columnsattribute ofFromClauseA
ColumnCollectionthat represents the “exported” columns of thisSelectable.The “exported” columns for a
FromClauseobject are synonymous with theFromClause.columnscollection.New in version 1.4.
-
attribute
sqlalchemy.sql.expression.TableClause.foreign_keys¶ inherited from the
FromClause.foreign_keysattribute ofFromClauseReturn the collection of
ForeignKeymarker objects which this FromClause references.Each
ForeignKeyis a member of aTable-wideForeignKeyConstraint.See also
-
method
sqlalchemy.sql.expression.TableClause.get_children(omit_attrs=(), **kw)¶ inherited from the
Traversible.get_children()method ofTraversibleReturn immediate child
Traversibleelements of thisTraversible.This is used for visit traversal.
**kw may contain flags that change the collection that is returned, for example to return a subset of items in order to cut down on larger traversals, or to return child items from a different context (such as schema-level collections instead of clause-level).
-
attribute
sqlalchemy.sql.expression.TableClause.implicit_returning = False¶ TableClausedoesn’t support having a primary key or column -level defaults, so implicit returning doesn’t apply.
-
attribute
sqlalchemy.sql.expression.TableClause.inherit_cache = None¶ inherited from the
HasCacheKey.inherit_cacheattribute ofHasCacheKeyIndicate if this
HasCacheKeyinstance should make use of the cache key generation scheme used by its immediate superclass.The attribute defaults to
None, which indicates that a construct has not yet taken into account whether or not its appropriate for it to participate in caching; this is functionally equivalent to setting the value toFalse, except that a warning is also emitted.This flag can be set to
Trueon a particular class, if the SQL that corresponds to the object does not change based on attributes which are local to this class, and not its superclass.See also
Enabling Caching Support for Custom Constructs - General guideslines for setting the
HasCacheKey.inherit_cacheattribute for third-party or user defined SQL constructs.
-
method
sqlalchemy.sql.expression.TableClause.insert(values=None, inline=False, **kwargs)¶ Generate an
insert()construct against thisTableClause.E.g.:
table.insert().values(name='foo')
See
insert()for argument and usage information.
-
method
sqlalchemy.sql.expression.TableClause.is_derived_from(fromclause)¶ inherited from the
FromClause.is_derived_from()method ofFromClauseReturn
Trueif thisFromClauseis ‘derived’ from the givenFromClause.An example would be an Alias of a Table is derived from that Table.
-
method
sqlalchemy.sql.expression.TableClause.join(right, onclause=None, isouter=False, full=False)¶ inherited from the
FromClause.join()method ofFromClauseReturn a
Joinfrom thisFromClauseto anotherFromClause.E.g.:
from sqlalchemy import join j = user_table.join(address_table, user_table.c.id == address_table.c.user_id) stmt = select(user_table).select_from(j)
would emit SQL along the lines of:
SELECT user.id, user.name FROM user JOIN address ON user.id = address.user_id
- Parameters:
right¶ – the right side of the join; this is any
FromClauseobject such as aTableobject, and may also be a selectable-compatible object such as an ORM-mapped class.onclause¶ – a SQL expression representing the ON clause of the join. If left at
None,FromClause.join()will attempt to join the two tables based on a foreign key relationship.isouter¶ – if True, render a LEFT OUTER JOIN, instead of JOIN.
full¶ –
if True, render a FULL OUTER JOIN, instead of LEFT OUTER JOIN. Implies
FromClause.join.isouter.New in version 1.1.
-
method
sqlalchemy.sql.expression.TableClause.lateral(name=None)¶ inherited from the
Selectable.lateral()method ofSelectableReturn a LATERAL alias of this
Selectable.The return value is the
Lateralconstruct also provided by the top-levellateral()function.New in version 1.1.
See also
LATERAL correlation - overview of usage.
-
classmethod
sqlalchemy.sql.expression.TableClause.memoized_instancemethod(fn)¶ inherited from the
HasMemoized.memoized_instancemethod()method ofHasMemoizedDecorate a method memoize its return value.
-
method
sqlalchemy.sql.expression.TableClause.outerjoin(right, onclause=None, full=False)¶ inherited from the
FromClause.outerjoin()method ofFromClauseReturn a
Joinfrom thisFromClauseto anotherFromClause, with the “isouter” flag set to True.E.g.:
from sqlalchemy import outerjoin j = user_table.outerjoin(address_table, user_table.c.id == address_table.c.user_id)
The above is equivalent to:
j = user_table.join( address_table, user_table.c.id == address_table.c.user_id, isouter=True)
- Parameters:
right¶ – the right side of the join; this is any
FromClauseobject such as aTableobject, and may also be a selectable-compatible object such as an ORM-mapped class.onclause¶ – a SQL expression representing the ON clause of the join. If left at
None,FromClause.join()will attempt to join the two tables based on a foreign key relationship.full¶ –
if True, render a FULL OUTER JOIN, instead of LEFT OUTER JOIN.
New in version 1.1.
-
method
sqlalchemy.sql.expression.TableClause.params(*optionaldict, **kwargs)¶ inherited from the
Immutable.params()method ofImmutableReturn a copy with
bindparam()elements replaced.Returns a copy of this ClauseElement with
bindparam()elements replaced with values taken from the given dictionary:>>> clause = column('x') + bindparam('foo') >>> print(clause.compile().params) {'foo':None} >>> print(clause.params({'foo':7}).compile().params) {'foo':7}
-
attribute
sqlalchemy.sql.expression.TableClause.primary_key¶ inherited from the
FromClause.primary_keyattribute ofFromClauseReturn the iterable collection of
Columnobjects which comprise the primary key of this_selectable.FromClause.For a
Tableobject, this collection is represented by thePrimaryKeyConstraintwhich itself is an iterable collection ofColumnobjects.
-
method
sqlalchemy.sql.expression.TableClause.replace_selectable(old, alias)¶ inherited from the
Selectable.replace_selectable()method ofSelectableReplace all occurrences of
FromClause‘old’ with the givenAliasobject, returning a copy of thisFromClause.Deprecated since version 1.4: The
Selectable.replace_selectable()method is deprecated, and will be removed in a future release. Similar functionality is available via the sqlalchemy.sql.visitors module.
-
attribute
sqlalchemy.sql.expression.TableClause.schema = None¶ inherited from the
FromClause.schemaattribute ofFromClauseDefine the ‘schema’ attribute for this
FromClause.This is typically
Nonefor most objects except that ofTable, where it is taken as the value of theTable.schemaargument.
-
method
sqlalchemy.sql.expression.TableClause.select(whereclause=None, **kwargs)¶ inherited from the
FromClause.select()method ofFromClauseReturn a SELECT of this
FromClause.e.g.:
stmt = some_table.select().where(some_table.c.id == 5)
- Parameters:
whereclause¶ –
a WHERE clause, equivalent to calling the
Select.where()method.Deprecated since version 1.4: The
FromClause.select().whereclauseparameter is deprecated and will be removed in version 2.0. Please make use of theSelect.where()method to add WHERE criteria to the SELECT statement.**kwargs¶ – additional keyword arguments are passed to the legacy constructor for
Selectdescribed atSelect.create_legacy_select().
See also
select()- general purpose method which allows for arbitrary column lists.
-
method
sqlalchemy.sql.expression.TableClause.self_group(against=None)¶ inherited from the
ClauseElement.self_group()method ofClauseElementApply a ‘grouping’ to this
ClauseElement.This method is overridden by subclasses to return a “grouping” construct, i.e. parenthesis. In particular it’s used by “binary” expressions to provide a grouping around themselves when placed into a larger expression, as well as by
select()constructs when placed into the FROM clause of anotherselect(). (Note that subqueries should be normally created using theSelect.alias()method, as many platforms require nested SELECT statements to be named).As expressions are composed together, the application of
self_group()is automatic - end-user code should never need to use this method directly. Note that SQLAlchemy’s clause constructs take operator precedence into account - so parenthesis might not be needed, for example, in an expression likex OR (y AND z)- AND takes precedence over OR.The base
self_group()method ofClauseElementjust returns self.
-
method
sqlalchemy.sql.expression.TableClause.table_valued()¶ inherited from the
FromClause.table_valued()method ofFromClauseReturn a
TableValuedColumnobject for thisFromClause.A
TableValuedColumnis aColumnElementthat represents a complete row in a table. Support for this construct is backend dependent, and is supported in various forms by backends such as PostgreSQL, Oracle and SQL Server.E.g.:
>>> from sqlalchemy import select, column, func, table >>> a = table("a", column("id"), column("x"), column("y")) >>> stmt = select(func.row_to_json(a.table_valued())) >>> print(stmt) SELECT row_to_json(a) AS row_to_json_1 FROM a
New in version 1.4.0b2.
See also
Working with SQL Functions - in the SQLAlchemy 1.4 / 2.0 Tutorial
-
method
sqlalchemy.sql.expression.TableClause.tablesample(sampling, name=None, seed=None)¶ inherited from the
FromClause.tablesample()method ofFromClauseReturn a TABLESAMPLE alias of this
FromClause.The return value is the
TableSampleconstruct also provided by the top-leveltablesample()function.New in version 1.1.
See also
tablesample()- usage guidelines and parameters
-
method
sqlalchemy.sql.expression.TableClause.unique_params(*optionaldict, **kwargs)¶ inherited from the
Immutable.unique_params()method ofImmutableReturn a copy with
bindparam()elements replaced.Same functionality as
ClauseElement.params(), except adds unique=True to affected bind parameters so that multiple statements can be used.
-
method
sqlalchemy.sql.expression.TableClause.update(whereclause=None, values=None, inline=False, **kwargs)¶ Generate an
update()construct against thisTableClause.E.g.:
table.update().where(table.c.id==7).values(name='foo')
See
update()for argument and usage information.
-
method
- class sqlalchemy.sql.expression.TableSample(*arg, **kw)¶
Represent a TABLESAMPLE clause.
This object is constructed from the
tablesample()module level function as well as theFromClause.tablesample()method available on allFromClausesubclasses.New in version 1.1.
See also
Class signature
class
sqlalchemy.sql.expression.TableSample(sqlalchemy.sql.expression.AliasedReturnsRows)
- class sqlalchemy.sql.expression.TableValuedAlias(*arg, **kw)¶
An alias against a “table valued” SQL function.
This construct provides for a SQL function that returns columns to be used in the FROM clause of a SELECT statement. The object is generated using the
FunctionElement.table_valued()method, e.g.:>>> from sqlalchemy import select, func >>> fn = func.json_array_elements_text('["one", "two", "three"]').table_valued("value") >>> print(select(fn.c.value)) SELECT anon_1.value FROM json_array_elements_text(:json_array_elements_text_1) AS anon_1
New in version 1.4.0b2.
See also
Table-Valued Functions - in the SQLAlchemy 1.4 / 2.0 Tutorial
Members
Class signature
class
sqlalchemy.sql.expression.TableValuedAlias(sqlalchemy.sql.expression.Alias)-
method
sqlalchemy.sql.expression.TableValuedAlias.alias(name=None)¶ Return a new alias of this
TableValuedAlias.This creates a distinct FROM object that will be distinguished from the original one when used in a SQL statement.
-
attribute
sqlalchemy.sql.expression.TableValuedAlias.column¶ Return a column expression representing this
TableValuedAlias.This accessor is used to implement the
FunctionElement.column_valued()method. See that method for further details.E.g.:
>>> print(select(func.some_func().table_valued("value").column)) SELECT anon_1 FROM some_func() AS anon_1
See also
-
method
sqlalchemy.sql.expression.TableValuedAlias.lateral(name=None)¶ Return a new
TableValuedAliaswith the lateral flag set, so that it renders as LATERAL.See also
-
method
sqlalchemy.sql.expression.TableValuedAlias.render_derived(name=None, with_types=False)¶ Apply “render derived” to this
TableValuedAlias.This has the effect of the individual column names listed out after the alias name in the “AS” sequence, e.g.:
>>> print( ... select( ... func.unnest(array(["one", "two", "three"])). table_valued("x", with_ordinality="o").render_derived() ... ) ... ) SELECT anon_1.x, anon_1.o FROM unnest(ARRAY[%(param_1)s, %(param_2)s, %(param_3)s]) WITH ORDINALITY AS anon_1(x, o)
The
with_typeskeyword will render column types inline within the alias expression (this syntax currently applies to the PostgreSQL database):>>> print( ... select( ... func.json_to_recordset( ... '[{"a":1,"b":"foo"},{"a":"2","c":"bar"}]' ... ) ... .table_valued(column("a", Integer), column("b", String)) ... .render_derived(with_types=True) ... ) ... ) SELECT anon_1.a, anon_1.b FROM json_to_recordset(:json_to_recordset_1) AS anon_1(a INTEGER, b VARCHAR)
- Parameters:
name¶ – optional string name that will be applied to the alias generated. If left as None, a unique anonymizing name will be used.
with_types¶ – if True, the derived columns will include the datatype specification with each column. This is a special syntax currently known to be required by PostgreSQL for some SQL functions.
-
method
- class sqlalchemy.sql.expression.TextualSelect(text, columns, positional=False)¶
Wrap a
TextClauseconstruct within aSelectBaseinterface.This allows the
TextClauseobject to gain a.ccollection and other FROM-like capabilities such asFromClause.alias(),SelectBase.cte(), etc.The
TextualSelectconstruct is produced via theTextClause.columns()method - see that method for details.Changed in version 1.4: the
TextualSelectclass was renamed fromTextAsFrom, to more correctly suit its role as a SELECT-oriented object and not a FROM clause.Members
add_cte(), alias(), as_scalar(), bind, c, compare(), compile(), corresponding_column(), cte(), execute(), execution_options(), exists(), exported_columns, get_children(), get_execution_options(), inherit_cache, label(), lateral(), memoized_instancemethod(), options(), params(), replace_selectable(), scalar(), scalar_subquery(), select(), selected_columns, self_group(), subquery(), unique_params()
Class signature
class
sqlalchemy.sql.expression.TextualSelect(sqlalchemy.sql.expression.SelectBase)-
method
sqlalchemy.sql.expression.TextualSelect.add_cte(cte)¶ inherited from the
HasCTE.add_cte()method ofHasCTEAdd a
CTEto this statement object that will be independently rendered even if not referenced in the statement otherwise.This feature is useful for the use case of embedding a DML statement such as an INSERT or UPDATE as a CTE inline with a primary statement that may draw from its results indirectly; while PostgreSQL is known to support this usage, it may not be supported by other backends.
E.g.:
from sqlalchemy import table, column, select t = table('t', column('c1'), column('c2')) ins = t.insert().values({"c1": "x", "c2": "y"}).cte() stmt = select(t).add_cte(ins)
Would render:
WITH anon_1 AS (INSERT INTO t (c1, c2) VALUES (:param_1, :param_2)) SELECT t.c1, t.c2 FROM t
Above, the “anon_1” CTE is not referred towards in the SELECT statement, however still accomplishes the task of running an INSERT statement.
Similarly in a DML-related context, using the PostgreSQL
Insertconstruct to generate an “upsert”:from sqlalchemy import table, column from sqlalchemy.dialects.postgresql import insert t = table("t", column("c1"), column("c2")) delete_statement_cte = ( t.delete().where(t.c.c1 < 1).cte("deletions") ) insert_stmt = insert(t).values({"c1": 1, "c2": 2}) update_statement = insert_stmt.on_conflict_do_update( index_elements=[t.c.c1], set_={ "c1": insert_stmt.excluded.c1, "c2": insert_stmt.excluded.c2, }, ).add_cte(delete_statement_cte) print(update_statement)
The above statement renders as:
WITH deletions AS (DELETE FROM t WHERE t.c1 < %(c1_1)s) INSERT INTO t (c1, c2) VALUES (%(c1)s, %(c2)s) ON CONFLICT (c1) DO UPDATE SET c1 = excluded.c1, c2 = excluded.c2
New in version 1.4.21.
-
method
sqlalchemy.sql.expression.TextualSelect.alias(name=None, flat=False)¶ inherited from the
SelectBase.alias()method ofSelectBaseReturn a named subquery against this
SelectBase.For a
SelectBase(as opposed to aFromClause), this returns aSubqueryobject which behaves mostly the same as theAliasobject that is used with aFromClause.Changed in version 1.4: The
SelectBase.alias()method is now a synonym for theSelectBase.subquery()method.
-
method
sqlalchemy.sql.expression.TextualSelect.as_scalar()¶ inherited from the
SelectBase.as_scalar()method ofSelectBaseDeprecated since version 1.4: The
SelectBase.as_scalar()method is deprecated and will be removed in a future release. Please refer toSelectBase.scalar_subquery().
-
attribute
sqlalchemy.sql.expression.TextualSelect.bind¶ inherited from the
Executable.bindattribute ofExecutableReturns the
EngineorConnectionto which thisExecutableis bound, or None if none found.Deprecated since version 1.4: The
Executable.bindattribute is considered legacy as of the 1.x series of SQLAlchemy and will be removed in 2.0. Bound metadata is being removed as of SQLAlchemy 2.0. (Background on SQLAlchemy 2.0 at: Migrating to SQLAlchemy 2.0)This is a traversal which checks locally, then checks among the “from” clauses of associated objects until a bound engine or connection is found.
-
attribute
sqlalchemy.sql.expression.TextualSelect.c¶ inherited from the
SelectBase.cattribute ofSelectBaseDeprecated since version 1.4: The
SelectBase.candSelectBase.columnsattributes are deprecated and will be removed in a future release; these attributes implicitly create a subquery that should be explicit. Please callSelectBase.subquery()first in order to create a subquery, which then contains this attribute. To access the columns that this SELECT object SELECTs from, use theSelectBase.selected_columnsattribute.
-
method
sqlalchemy.sql.expression.TextualSelect.compare(other, **kw)¶ inherited from the
ClauseElement.compare()method ofClauseElementCompare this
ClauseElementto the givenClauseElement.Subclasses should override the default behavior, which is a straight identity comparison.
**kw are arguments consumed by subclass
compare()methods and may be used to modify the criteria for comparison (seeColumnElement).
-
method
sqlalchemy.sql.expression.TextualSelect.compile(bind=None, dialect=None, **kw)¶ inherited from the
ClauseElement.compile()method ofClauseElementCompile this SQL expression.
The return value is a
Compiledobject. Callingstr()orunicode()on the returned value will yield a string representation of the result. TheCompiledobject also can return a dictionary of bind parameter names and values using theparamsaccessor.- Parameters:
bind¶ – An
EngineorConnectionfrom which aCompiledwill be acquired. This argument takes precedence over thisClauseElement’s bound engine, if any.column_keys¶ – Used for INSERT and UPDATE statements, a list of column names which should be present in the VALUES clause of the compiled statement. If
None, all columns from the target table object are rendered.dialect¶ – A
Dialectinstance from which aCompiledwill be acquired. This argument takes precedence over the bind argument as well as thisClauseElement‘s bound engine, if any.compile_kwargs¶ –
optional dictionary of additional parameters that will be passed through to the compiler within all “visit” methods. This allows any custom flag to be passed through to a custom compilation construct, for example. It is also used for the case of passing the
literal_bindsflag through:from sqlalchemy.sql import table, column, select t = table('t', column('x')) s = select(t).where(t.c.x == 5) print(s.compile(compile_kwargs={"literal_binds": True}))
New in version 0.9.0.
-
method
sqlalchemy.sql.expression.TextualSelect.corresponding_column(column, require_embedded=False)¶ inherited from the
Selectable.corresponding_column()method ofSelectableGiven a
ColumnElement, return the exportedColumnElementobject from theSelectable.exported_columnscollection of thisSelectablewhich corresponds to that originalColumnElementvia a common ancestor column.- Parameters:
column¶ – the target
ColumnElementto be matched.require_embedded¶ – only return corresponding columns for the given
ColumnElement, if the givenColumnElementis actually present within a sub-element of thisSelectable. Normally the column will match if it merely shares a common ancestor with one of the exported columns of thisSelectable.
See also
Selectable.exported_columns- theColumnCollectionthat is used for the operation.ColumnCollection.corresponding_column()- implementation method.
-
method
sqlalchemy.sql.expression.TextualSelect.cte(name=None, recursive=False, nesting=False)¶ inherited from the
HasCTE.cte()method ofHasCTEReturn a new
CTE, or Common Table Expression instance.Common table expressions are a SQL standard whereby SELECT statements can draw upon secondary statements specified along with the primary statement, using a clause called “WITH”. Special semantics regarding UNION can also be employed to allow “recursive” queries, where a SELECT statement can draw upon the set of rows that have previously been selected.
CTEs can also be applied to DML constructs UPDATE, INSERT and DELETE on some databases, both as a source of CTE rows when combined with RETURNING, as well as a consumer of CTE rows.
Changed in version 1.1: Added support for UPDATE/INSERT/DELETE as CTE, CTEs added to UPDATE/INSERT/DELETE.
SQLAlchemy detects
CTEobjects, which are treated similarly toAliasobjects, as special elements to be delivered to the FROM clause of the statement as well as to a WITH clause at the top of the statement.For special prefixes such as PostgreSQL “MATERIALIZED” and “NOT MATERIALIZED”, the
CTE.prefix_with()method may be used to establish these.Changed in version 1.3.13: Added support for prefixes. In particular - MATERIALIZED and NOT MATERIALIZED.
- Parameters:
name¶ – name given to the common table expression. Like
FromClause.alias(), the name can be left asNonein which case an anonymous symbol will be used at query compile time.recursive¶ – if
True, will renderWITH RECURSIVE. A recursive common table expression is intended to be used in conjunction with UNION ALL in order to derive rows from those already selected.nesting¶ –
if
True, will render the CTE locally to the actual statement.New in version 1.4.24.
The following examples include two from PostgreSQL’s documentation at https://www.postgresql.org/docs/current/static/queries-with.html, as well as additional examples.
Example 1, non recursive:
from sqlalchemy import (Table, Column, String, Integer, MetaData, select, func) metadata = MetaData() orders = Table('orders', metadata, Column('region', String), Column('amount', Integer), Column('product', String), Column('quantity', Integer) ) regional_sales = select( orders.c.region, func.sum(orders.c.amount).label('total_sales') ).group_by(orders.c.region).cte("regional_sales") top_regions = select(regional_sales.c.region).\ where( regional_sales.c.total_sales > select( func.sum(regional_sales.c.total_sales) / 10 ) ).cte("top_regions") statement = select( orders.c.region, orders.c.product, func.sum(orders.c.quantity).label("product_units"), func.sum(orders.c.amount).label("product_sales") ).where(orders.c.region.in_( select(top_regions.c.region) )).group_by(orders.c.region, orders.c.product) result = conn.execute(statement).fetchall()
Example 2, WITH RECURSIVE:
from sqlalchemy import (Table, Column, String, Integer, MetaData, select, func) metadata = MetaData() parts = Table('parts', metadata, Column('part', String), Column('sub_part', String), Column('quantity', Integer), ) included_parts = select(\ parts.c.sub_part, parts.c.part, parts.c.quantity\ ).\ where(parts.c.part=='our part').\ cte(recursive=True) incl_alias = included_parts.alias() parts_alias = parts.alias() included_parts = included_parts.union_all( select( parts_alias.c.sub_part, parts_alias.c.part, parts_alias.c.quantity ).\ where(parts_alias.c.part==incl_alias.c.sub_part) ) statement = select( included_parts.c.sub_part, func.sum(included_parts.c.quantity). label('total_quantity') ).\ group_by(included_parts.c.sub_part) result = conn.execute(statement).fetchall()
Example 3, an upsert using UPDATE and INSERT with CTEs:
from datetime import date from sqlalchemy import (MetaData, Table, Column, Integer, Date, select, literal, and_, exists) metadata = MetaData() visitors = Table('visitors', metadata, Column('product_id', Integer, primary_key=True), Column('date', Date, primary_key=True), Column('count', Integer), ) # add 5 visitors for the product_id == 1 product_id = 1 day = date.today() count = 5 update_cte = ( visitors.update() .where(and_(visitors.c.product_id == product_id, visitors.c.date == day)) .values(count=visitors.c.count + count) .returning(literal(1)) .cte('update_cte') ) upsert = visitors.insert().from_select( [visitors.c.product_id, visitors.c.date, visitors.c.count], select(literal(product_id), literal(day), literal(count)) .where(~exists(update_cte.select())) ) connection.execute(upsert)
Example 4, Nesting CTE (SQLAlchemy 1.4.24 and above):
value_a = select( literal("root").label("n") ).cte("value_a") # A nested CTE with the same name as the root one value_a_nested = select( literal("nesting").label("n") ).cte("value_a", nesting=True) # Nesting CTEs takes ascendency locally # over the CTEs at a higher level value_b = select(value_a_nested.c.n).cte("value_b") value_ab = select(value_a.c.n.label("a"), value_b.c.n.label("b"))
The above query will render the second CTE nested inside the first, shown with inline parameters below as:
WITH value_a AS (SELECT 'root' AS n), value_b AS (WITH value_a AS (SELECT 'nesting' AS n) SELECT value_a.n AS n FROM value_a) SELECT value_a.n AS a, value_b.n AS b FROM value_a, value_b
Example 5, Non-Linear CTE (SQLAlchemy 1.4.28 and above):
edge = Table( "edge", metadata, Column("id", Integer, primary_key=True), Column("left", Integer), Column("right", Integer), ) root_node = select(literal(1).label("node")).cte( "nodes", recursive=True ) left_edge = select(edge.c.left).join( root_node, edge.c.right == root_node.c.node ) right_edge = select(edge.c.right).join( root_node, edge.c.left == root_node.c.node ) subgraph_cte = root_node.union(left_edge, right_edge) subgraph = select(subgraph_cte)
The above query will render 2 UNIONs inside the recursive CTE:
WITH RECURSIVE nodes(node) AS ( SELECT 1 AS node UNION SELECT edge."left" AS "left" FROM edge JOIN nodes ON edge."right" = nodes.node UNION SELECT edge."right" AS "right" FROM edge JOIN nodes ON edge."left" = nodes.node ) SELECT nodes.node FROM nodes
See also
Query.cte()- ORM version ofHasCTE.cte().
-
method
sqlalchemy.sql.expression.TextualSelect.execute(*multiparams, **params)¶ inherited from the
Executable.execute()method ofExecutableCompile and execute this
Executable.Deprecated since version 1.4: The
Executable.execute()method is considered legacy as of the 1.x series of SQLAlchemy and will be removed in 2.0. All statement execution in SQLAlchemy 2.0 is performed by theConnection.execute()method ofConnection, or in the ORM by theSession.execute()method ofSession. (Background on SQLAlchemy 2.0 at: Migrating to SQLAlchemy 2.0)
-
method
sqlalchemy.sql.expression.TextualSelect.execution_options(**kw)¶ inherited from the
Executable.execution_options()method ofExecutableSet non-SQL options for the statement which take effect during execution.
Execution options can be set on a per-statement or per
Connectionbasis. Additionally, theEngineand ORMQueryobjects provide access to execution options which they in turn configure upon connections.The
execution_options()method is generative. A new instance of this statement is returned that contains the options:statement = select(table.c.x, table.c.y) statement = statement.execution_options(autocommit=True)
Note that only a subset of possible execution options can be applied to a statement - these include “autocommit” and “stream_results”, but not “isolation_level” or “compiled_cache”. See
Connection.execution_options()for a full list of possible options.
-
method
sqlalchemy.sql.expression.TextualSelect.exists()¶ inherited from the
SelectBase.exists()method ofSelectBaseReturn an
Existsrepresentation of this selectable, which can be used as a column expression.The returned object is an instance of
Exists.New in version 1.4.
-
attribute
sqlalchemy.sql.expression.TextualSelect.exported_columns¶ inherited from the
SelectBase.exported_columnsattribute ofSelectBaseA
ColumnCollectionthat represents the “exported” columns of thisSelectable, not includingTextClauseconstructs.The “exported” columns for a
SelectBaseobject are synonymous with theSelectBase.selected_columnscollection.New in version 1.4.
-
method
sqlalchemy.sql.expression.TextualSelect.get_children(omit_attrs=(), **kw)¶ inherited from the
Traversible.get_children()method ofTraversibleReturn immediate child
Traversibleelements of thisTraversible.This is used for visit traversal.
**kw may contain flags that change the collection that is returned, for example to return a subset of items in order to cut down on larger traversals, or to return child items from a different context (such as schema-level collections instead of clause-level).
-
method
sqlalchemy.sql.expression.TextualSelect.get_execution_options()¶ inherited from the
Executable.get_execution_options()method ofExecutableGet the non-SQL options which will take effect during execution.
New in version 1.3.
See also
-
attribute
sqlalchemy.sql.expression.TextualSelect.inherit_cache = None¶ inherited from the
HasCacheKey.inherit_cacheattribute ofHasCacheKeyIndicate if this
HasCacheKeyinstance should make use of the cache key generation scheme used by its immediate superclass.The attribute defaults to
None, which indicates that a construct has not yet taken into account whether or not its appropriate for it to participate in caching; this is functionally equivalent to setting the value toFalse, except that a warning is also emitted.This flag can be set to
Trueon a particular class, if the SQL that corresponds to the object does not change based on attributes which are local to this class, and not its superclass.See also
Enabling Caching Support for Custom Constructs - General guideslines for setting the
HasCacheKey.inherit_cacheattribute for third-party or user defined SQL constructs.
-
method
sqlalchemy.sql.expression.TextualSelect.label(name)¶ inherited from the
SelectBase.label()method ofSelectBaseReturn a ‘scalar’ representation of this selectable, embedded as a subquery with a label.
See also
-
method
sqlalchemy.sql.expression.TextualSelect.lateral(name=None)¶ inherited from the
SelectBase.lateral()method ofSelectBaseReturn a LATERAL alias of this
Selectable.The return value is the
Lateralconstruct also provided by the top-levellateral()function.New in version 1.1.
See also
LATERAL correlation - overview of usage.
-
classmethod
sqlalchemy.sql.expression.TextualSelect.memoized_instancemethod(fn)¶ inherited from the
HasMemoized.memoized_instancemethod()method ofHasMemoizedDecorate a method memoize its return value.
-
method
sqlalchemy.sql.expression.TextualSelect.options(*options)¶ inherited from the
Executable.options()method ofExecutableApply options to this statement.
In the general sense, options are any kind of Python object that can be interpreted by the SQL compiler for the statement. These options can be consumed by specific dialects or specific kinds of compilers.
The most commonly known kind of option are the ORM level options that apply “eager load” and other loading behaviors to an ORM query. However, options can theoretically be used for many other purposes.
For background on specific kinds of options for specific kinds of statements, refer to the documentation for those option objects.
Changed in version 1.4: - added
Generative.options()to Core statement objects towards the goal of allowing unified Core / ORM querying capabilities.See also
Deferred Column Loader Query Options - refers to options specific to the usage of ORM queries
Relationship Loading with Loader Options - refers to options specific to the usage of ORM queries
-
method
sqlalchemy.sql.expression.TextualSelect.params(*optionaldict, **kwargs)¶ inherited from the
ClauseElement.params()method ofClauseElementReturn a copy with
bindparam()elements replaced.Returns a copy of this ClauseElement with
bindparam()elements replaced with values taken from the given dictionary:>>> clause = column('x') + bindparam('foo') >>> print(clause.compile().params) {'foo':None} >>> print(clause.params({'foo':7}).compile().params) {'foo':7}
-
method
sqlalchemy.sql.expression.TextualSelect.replace_selectable(old, alias)¶ inherited from the
Selectable.replace_selectable()method ofSelectableReplace all occurrences of
FromClause‘old’ with the givenAliasobject, returning a copy of thisFromClause.Deprecated since version 1.4: The
Selectable.replace_selectable()method is deprecated, and will be removed in a future release. Similar functionality is available via the sqlalchemy.sql.visitors module.
-
method
sqlalchemy.sql.expression.TextualSelect.scalar(*multiparams, **params)¶ inherited from the
Executable.scalar()method ofExecutableCompile and execute this
Executable, returning the result’s scalar representation.Deprecated since version 1.4: The
Executable.scalar()method is considered legacy as of the 1.x series of SQLAlchemy and will be removed in 2.0. Scalar execution in SQLAlchemy 2.0 is performed by theConnection.scalar()method ofConnection, or in the ORM by theSession.scalar()method ofSession. (Background on SQLAlchemy 2.0 at: Migrating to SQLAlchemy 2.0)
-
method
sqlalchemy.sql.expression.TextualSelect.scalar_subquery()¶ inherited from the
SelectBase.scalar_subquery()method ofSelectBaseReturn a ‘scalar’ representation of this selectable, which can be used as a column expression.
The returned object is an instance of
ScalarSelect.Typically, a select statement which has only one column in its columns clause is eligible to be used as a scalar expression. The scalar subquery can then be used in the WHERE clause or columns clause of an enclosing SELECT.
Note that the scalar subquery differentiates from the FROM-level subquery that can be produced using the
SelectBase.subquery()method.See also
Scalar and Correlated Subqueries - in the 2.0 tutorial
-
method
sqlalchemy.sql.expression.TextualSelect.select(*arg, **kw)¶ inherited from the
SelectBase.select()method ofSelectBaseDeprecated since version 1.4: The
SelectBase.select()method is deprecated and will be removed in a future release; this method implicitly creates a subquery that should be explicit. Please callSelectBase.subquery()first in order to create a subquery, which then can be selected.
-
attribute
sqlalchemy.sql.expression.TextualSelect.selected_columns¶ A
ColumnCollectionrepresenting the columns that this SELECT statement or similar construct returns in its result set, not includingTextClauseconstructs.This collection differs from the
FromClause.columnscollection of aFromClausein that the columns within this collection cannot be directly nested inside another SELECT statement; a subquery must be applied first which provides for the necessary parenthesization required by SQL.For a
TextualSelectconstruct, the collection contains theColumnElementobjects that were passed to the constructor, typically via theTextClause.columns()method.New in version 1.4.
-
method
sqlalchemy.sql.expression.TextualSelect.self_group(against=None)¶ inherited from the
ClauseElement.self_group()method ofClauseElementApply a ‘grouping’ to this
ClauseElement.This method is overridden by subclasses to return a “grouping” construct, i.e. parenthesis. In particular it’s used by “binary” expressions to provide a grouping around themselves when placed into a larger expression, as well as by
select()constructs when placed into the FROM clause of anotherselect(). (Note that subqueries should be normally created using theSelect.alias()method, as many platforms require nested SELECT statements to be named).As expressions are composed together, the application of
self_group()is automatic - end-user code should never need to use this method directly. Note that SQLAlchemy’s clause constructs take operator precedence into account - so parenthesis might not be needed, for example, in an expression likex OR (y AND z)- AND takes precedence over OR.The base
self_group()method ofClauseElementjust returns self.
-
method
sqlalchemy.sql.expression.TextualSelect.subquery(name=None)¶ inherited from the
SelectBase.subquery()method ofSelectBaseReturn a subquery of this
SelectBase.A subquery is from a SQL perspective a parenthesized, named construct that can be placed in the FROM clause of another SELECT statement.
Given a SELECT statement such as:
stmt = select(table.c.id, table.c.name)
The above statement might look like:
SELECT table.id, table.name FROM table
The subquery form by itself renders the same way, however when embedded into the FROM clause of another SELECT statement, it becomes a named sub-element:
subq = stmt.subquery() new_stmt = select(subq)
The above renders as:
SELECT anon_1.id, anon_1.name FROM (SELECT table.id, table.name FROM table) AS anon_1
Historically,
SelectBase.subquery()is equivalent to calling theFromClause.alias()method on a FROM object; however, as aSelectBaseobject is not directly FROM object, theSelectBase.subquery()method provides clearer semantics.New in version 1.4.
-
method
sqlalchemy.sql.expression.TextualSelect.unique_params(*optionaldict, **kwargs)¶ inherited from the
ClauseElement.unique_params()method ofClauseElementReturn a copy with
bindparam()elements replaced.Same functionality as
ClauseElement.params(), except adds unique=True to affected bind parameters so that multiple statements can be used.
-
method
- class sqlalchemy.sql.expression.Values(*columns, **kw)¶
Represent a
VALUESconstruct that can be used as a FROM element in a statement.The
Valuesobject is created from thevalues()function.New in version 1.4.
Members
Class signature
class
sqlalchemy.sql.expression.Values(sqlalchemy.sql.expression.Generative,sqlalchemy.sql.expression.FromClause)-
method
sqlalchemy.sql.expression.Values.__init__(*columns, **kw)¶ Construct a new
Valuesobject.This constructor is mirrored as a public API function; see
sqlalchemy.sql.expression.values()for a full usage and argument description.
-
method
sqlalchemy.sql.expression.Values.alias(name, **kw)¶ Return a new
Valuesconstruct that is a copy of this one with the given name.This method is a VALUES-specific specialization of the
FromClause.alias()method.
-
method
sqlalchemy.sql.expression.Values.data(values)¶ Return a new
Valuesconstruct, adding the given data to the data list.E.g.:
my_values = my_values.data([(1, 'value 1'), (2, 'value2')])
-
method
sqlalchemy.sql.expression.Values.lateral(name=None)¶ Return a new
Valueswith the lateral flag set, so that it renders as LATERAL.See also
-
method
Label Style Constants¶
Constants used with the GenerativeSelect.set_label_style()
method.
| Object Name | Description |
|---|---|
The default label style, refers to |
|
Label style indicating that columns with a name that conflicts with an existing name should be labeled with a semi-anonymizing label when generating the columns clause of a SELECT statement. |
|
Label style indicating no automatic labeling should be applied to the columns clause of a SELECT statement. |
|
Label style indicating all columns should be labeled as
|
- sqlalchemy.sql.expression.LABEL_STYLE_DISAMBIGUATE_ONLY = symbol('LABEL_STYLE_DISAMBIGUATE_ONLY')¶
Label style indicating that columns with a name that conflicts with an existing name should be labeled with a semi-anonymizing label when generating the columns clause of a SELECT statement.
Below, most column names are left unaffected, except for the second occurrence of the name
columna, which is labeled using the labelcolumna_1to disambiguate it from that oftablea.columna:>>> from sqlalchemy import table, column, select, true, LABEL_STYLE_DISAMBIGUATE_ONLY >>> table1 = table("table1", column("columna"), column("columnb")) >>> table2 = table("table2", column("columna"), column("columnc")) >>> print(select(table1, table2).join(table2, true()).set_label_style(LABEL_STYLE_DISAMBIGUATE_ONLY)) SELECT table1.columna, table1.columnb, table2.columna AS columna_1, table2.columnc FROM table1 JOIN table2 ON true
Used with the
GenerativeSelect.set_label_style()method,LABEL_STYLE_DISAMBIGUATE_ONLYis the default labeling style for all SELECT statements outside of 1.x style ORM queries.New in version 1.4.
- sqlalchemy.sql.expression.LABEL_STYLE_NONE = symbol('LABEL_STYLE_NONE')¶
Label style indicating no automatic labeling should be applied to the columns clause of a SELECT statement.
Below, the columns named
columnaare both rendered as is, meaning that the namecolumnacan only refer to the first occurrence of this name within a result set, as well as if the statement were used as a subquery:>>> from sqlalchemy import table, column, select, true, LABEL_STYLE_NONE >>> table1 = table("table1", column("columna"), column("columnb")) >>> table2 = table("table2", column("columna"), column("columnc")) >>> print(select(table1, table2).join(table2, true()).set_label_style(LABEL_STYLE_NONE)) SELECT table1.columna, table1.columnb, table2.columna, table2.columnc FROM table1 JOIN table2 ON true
Used with the
Select.set_label_style()method.New in version 1.4.
- sqlalchemy.sql.expression.LABEL_STYLE_TABLENAME_PLUS_COL = symbol('LABEL_STYLE_TABLENAME_PLUS_COL')¶
Label style indicating all columns should be labeled as
<tablename>_<columnname>when generating the columns clause of a SELECT statement, to disambiguate same-named columns referenced from different tables, aliases, or subqueries.Below, all column names are given a label so that the two same-named columns
columnaare disambiguated astable1_columnaandtable2_columna:>>> from sqlalchemy import table, column, select, true, LABEL_STYLE_TABLENAME_PLUS_COL >>> table1 = table("table1", column("columna"), column("columnb")) >>> table2 = table("table2", column("columna"), column("columnc")) >>> print(select(table1, table2).join(table2, true()).set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL)) SELECT table1.columna AS table1_columna, table1.columnb AS table1_columnb, table2.columna AS table2_columna, table2.columnc AS table2_columnc FROM table1 JOIN table2 ON true
Used with the
GenerativeSelect.set_label_style()method. Equivalent to the legacy methodSelect.apply_labels();LABEL_STYLE_TABLENAME_PLUS_COLis SQLAlchemy’s legacy auto-labeling style.LABEL_STYLE_DISAMBIGUATE_ONLYprovides a less intrusive approach to disambiguation of same-named column expressions.New in version 1.4.
- sqlalchemy.sql.expression.LABEL_STYLE_DEFAULT¶
The default label style, refers to
LABEL_STYLE_DISAMBIGUATE_ONLY.New in version 1.4.