Insert, Updates, Deletes¶
INSERT, UPDATE and DELETE statements build on a hierarchy starting
with UpdateBase
. The Insert
and Update
constructs build on the intermediary ValuesBase
.
Object Name | Description |
---|---|
delete(table[, whereclause, bind, returning, ...], **dialect_kw) |
Construct |
Represent a DELETE construct. |
|
insert(table[, values, inline, bind, ...], **dialect_kw) |
Construct an |
Represent an INSERT construct. |
|
update(table[, whereclause, values, inline, ...], **dialect_kw) |
Construct an |
Represent an Update construct. |
|
Form the base for |
|
Supplies support for |
- function sqlalchemy.sql.expression.delete(table, whereclause=None, bind=None, returning=None, prefixes=None, **dialect_kw)¶
Construct
Delete
object.Similar functionality is available via the
TableClause.delete()
method onTable
.- Parameters:
table – The table to delete rows from.
whereclause –
- A
ClauseElement
describing theWHERE
condition of the
DELETE
statement. Note that theDelete.where()
generative method may be used instead.
The WHERE clause can refer to multiple tables. For databases which support this, a
DELETE..USING
or similar clause will be generated. The statement will fail on databases that don’t have support for multi-table delete statements. A SQL-standard method of referring to additional tables in the WHERE clause is to use a correlated subquery:users.delete().where( users.c.name==select([addresses.c.email_address]). where(addresses.c.user_id==users.c.id). as_scalar() )
Changed in version 1.2.0: The WHERE clause of DELETE can refer to multiple tables.
- A
See also
Deletes - SQL Expression Tutorial
- function sqlalchemy.sql.expression.insert(table, values=None, inline=False, bind=None, prefixes=None, returning=None, return_defaults=False, **dialect_kw)¶
Construct an
Insert
object.Similar functionality is available via the
TableClause.insert()
method onTable
.- Parameters:
table –
TableClause
which is the subject of the insert.values – collection of values to be inserted; see
Insert.values()
for a description of allowed formats here. Can be omitted entirely; aInsert
construct will also dynamically render the VALUES clause at execution time based on the parameters passed toConnection.execute()
.inline – if True, no attempt will be made to retrieve the SQL-generated default values to be provided within the statement; in particular, this allows SQL expressions to be rendered ‘inline’ within the statement without the need to pre-execute them beforehand; for backends that support “returning”, this turns off the “implicit returning” feature for the statement.
If both values and compile-time bind parameters are present, the compile-time bind parameters override the information specified within values on a per-key basis.
The keys within values can be either
Column
objects or their string identifiers. Each key may reference one of:a literal data value (i.e. string, number, etc.);
a Column object;
a SELECT statement.
If a
SELECT
statement is specified which references thisINSERT
statement’s table, the statement will be correlated against theINSERT
statement.See also
Insert Expressions - SQL Expression Tutorial
Inserts, Updates and Deletes - SQL Expression Tutorial
- function sqlalchemy.sql.expression.update(table, whereclause=None, values=None, inline=False, bind=None, prefixes=None, returning=None, return_defaults=False, preserve_parameter_order=False, **dialect_kw)¶
Construct an
Update
object.E.g.:
from sqlalchemy import update stmt = update(users).where(users.c.id==5).\ values(name='user #5')
Similar functionality is available via the
TableClause.update()
method onTable
:stmt = users.update().\ where(users.c.id==5).\ values(name='user #5')
- Parameters:
table – A
Table
object representing the database table to be updated.whereclause –
Optional SQL expression describing the
WHERE
condition of theUPDATE
statement. Modern applications may prefer to use the generativeUpdate.where()
method to specify theWHERE
clause.The WHERE clause can refer to multiple tables. For databases which support this, an
UPDATE FROM
clause will be generated, or on MySQL, a multi-table update. The statement will fail on databases that don’t have support for multi-table update statements. A SQL-standard method of referring to additional tables in the WHERE clause is to use a correlated subquery:users.update().values(name='ed').where( users.c.name==select([addresses.c.email_address]).\ where(addresses.c.user_id==users.c.id).\ as_scalar() )
values –
Optional dictionary which specifies the
SET
conditions of theUPDATE
. If left asNone
, theSET
conditions are determined from those parameters passed to the statement during the execution and/or compilation of the statement. When compiled standalone without any parameters, theSET
clause generates for all columns.Modern applications may prefer to use the generative
Update.values()
method to set the values of the UPDATE statement.inline – if True, SQL defaults present on
Column
objects via thedefault
keyword will be compiled ‘inline’ into the statement and not pre-executed. This means that their values will not be available in the dictionary returned fromResultProxy.last_updated_params()
.preserve_parameter_order –
if True, the update statement is expected to receive parameters only via the
Update.values()
method, and they must be passed as a Pythonlist
of 2-tuples. The rendered UPDATE statement will emit the SET clause for each referenced column maintaining this order.New in version 1.0.10.
See also
Parameter-Ordered Updates - full example of the
update.preserve_parameter_order
flag
If both
values
and compile-time bind parameters are present, the compile-time bind parameters override the information specified withinvalues
on a per-key basis.The keys within
values
can be eitherColumn
objects or their string identifiers (specifically the “key” of theColumn
, normally but not necessarily equivalent to its “name”). Normally, theColumn
objects used here are expected to be part of the targetTable
that is the table to be updated. However when using MySQL, a multiple-table UPDATE statement can refer to columns from any of the tables referred to in the WHERE clause.The values referred to in
values
are typically:a literal data value (i.e. string, number, etc.)
a SQL expression, such as a related
Column
, a scalar-returningselect()
construct, etc.
When combining
select()
constructs within the values clause of anupdate()
construct, the subquery represented by theselect()
should be correlated to the parent table, that is, providing criterion which links the table inside the subquery to the outer table being updated:users.update().values( name=select([addresses.c.email_address]).\ where(addresses.c.user_id==users.c.id).\ as_scalar() )
See also
Inserts, Updates and Deletes - SQL Expression Language Tutorial
- class sqlalchemy.sql.expression.Delete(table, whereclause=None, bind=None, returning=None, prefixes=None, **dialect_kw)¶
Represent a DELETE construct.
The
Delete
object is created using thedelete()
function.Members
__init__(), argument_for(), bind, compare(), compile(), cte(), dialect_kwargs, dialect_options, execute(), execution_options(), get_children(), kwargs, params(), prefix_with(), returning(), scalar(), self_group(), unique_params(), where(), with_hint()
Class signature
class
sqlalchemy.sql.expression.Delete
(sqlalchemy.sql.expression.UpdateBase
)-
method
sqlalchemy.sql.expression.Delete.
__init__(table, whereclause=None, bind=None, returning=None, prefixes=None, **dialect_kw)¶ Construct a new
Delete
object.This constructor is mirrored as a public API function; see
delete()
for a full usage and argument description.
-
classmethod
sqlalchemy.sql.expression.Delete.
argument_for(dialect_name, argument_name, default)¶ inherited from the
DialectKWArgs.argument_for()
method ofDialectKWArgs
Add a new kind of dialect-specific keyword argument for this class.
E.g.:
Index.argument_for("mydialect", "length", None) some_index = Index('a', 'b', mydialect_length=5)
The
DialectKWArgs.argument_for()
method is a per-argument way adding extra arguments to theDefaultDialect.construct_arguments
dictionary. This dictionary provides a list of argument names accepted by various schema-level constructs on behalf of a dialect.New dialects should typically specify this dictionary all at once as a data member of the dialect class. The use case for ad-hoc addition of argument names is typically for end-user code that is also using a custom compilation scheme which consumes the additional arguments.
- Parameters:
dialect_name – name of a dialect. The dialect must be locatable, else a
NoSuchModuleError
is raised. The dialect must also include an existingDefaultDialect.construct_arguments
collection, indicating that it participates in the keyword-argument validation and default system, elseArgumentError
is raised. If the dialect does not include this collection, then any keyword argument can be specified on behalf of this dialect already. All dialects packaged within SQLAlchemy include this collection, however for third party dialects, support may vary.argument_name – name of the parameter.
default – default value of the parameter.
New in version 0.9.4.
-
attribute
sqlalchemy.sql.expression.Delete.
bind¶ inherited from the
UpdateBase.bind
attribute ofUpdateBase
Return a ‘bind’ linked to this
UpdateBase
or aTable
associated with it.
-
method
sqlalchemy.sql.expression.Delete.
compare(other, **kw)¶ inherited from the
ClauseElement.compare()
method ofClauseElement
Compare this ClauseElement to the given ClauseElement.
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. (see
ColumnElement
)
-
method
sqlalchemy.sql.expression.Delete.
compile(default, bind=None, dialect=None, **kw)¶ inherited from the
ClauseElement.compile()
method ofClauseElement
Compile this SQL expression.
The return value is a
Compiled
object. Callingstr()
orunicode()
on the returned value will yield a string representation of the result. TheCompiled
object also can return a dictionary of bind parameter names and values using theparams
accessor.- Parameters:
bind – An
Engine
orConnection
from which aCompiled
will 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
Dialect
instance from which aCompiled
will be acquired. This argument takes precedence over the bind argument as well as thisClauseElement
’s bound engine, if any.inline – Used for INSERT statements, for a dialect which does not support inline retrieval of newly generated primary key columns, will force the expression used to create the new primary key value to be rendered inline within the INSERT statement’s VALUES clause. This typically refers to Sequence execution but may also refer to any server-side default generation function associated with a primary key Column.
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_binds
flag 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.Delete.
cte(name=None, recursive=False)¶ inherited from the
HasCTE.cte()
method ofHasCTE
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.
SQLAlchemy detects
CTE
objects, which are treated similarly toAlias
objects, 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.Changed in version 1.1: Added support for UPDATE/INSERT/DELETE as CTE, CTEs added to UPDATE/INSERT/DELETE.
- Parameters:
name – name given to the common table expression. Like
_FromClause.alias()
, the name can be left asNone
in 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.
The following examples include two from PostgreSQL’s documentation at http://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)
See also
Query.cte()
- ORM version ofHasCTE.cte()
.
-
attribute
sqlalchemy.sql.expression.Delete.
dialect_kwargs¶ inherited from the
DialectKWArgs.dialect_kwargs
attribute ofDialectKWArgs
A collection of keyword arguments specified as dialect-specific options to this construct.
The arguments are present here in their original
<dialect>_<kwarg>
format. Only arguments that were actually passed are included; unlike theDialectKWArgs.dialect_options
collection, which contains all options known by this dialect including defaults.The collection is also writable; keys are accepted of the form
<dialect>_<kwarg>
where the value will be assembled into the list of options.New in version 0.9.2.
Changed in version 0.9.4: The
DialectKWArgs.dialect_kwargs
collection is now writable.See also
DialectKWArgs.dialect_options
- nested dictionary form
-
attribute
sqlalchemy.sql.expression.Delete.
dialect_options¶ inherited from the
DialectKWArgs.dialect_options
attribute ofDialectKWArgs
A collection of keyword arguments specified as dialect-specific options to this construct.
This is a two-level nested registry, keyed to
<dialect_name>
and<argument_name>
. For example, thepostgresql_where
argument would be locatable as:arg = my_object.dialect_options['postgresql']['where']
New in version 0.9.2.
See also
DialectKWArgs.dialect_kwargs
- flat dictionary form
-
method
sqlalchemy.sql.expression.Delete.
execute(*multiparams, **params)¶ inherited from the
Executable.execute()
method ofExecutable
Compile and execute this
Executable
.
-
method
sqlalchemy.sql.expression.Delete.
execution_options(**kw)¶ inherited from the
Executable.execution_options()
method ofExecutable
Set non-SQL options for the statement which take effect during execution.
Execution options can be set on a per-statement or per
Connection
basis. Additionally, theEngine
and ORMQuery
objects 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.Delete.
get_children(**kwargs)¶ Return immediate child elements of this
ClauseElement
.This is used for visit traversal.
**kwargs 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.Delete.
kwargs¶ inherited from the
DialectKWArgs.kwargs
attribute ofDialectKWArgs
A synonym for
DialectKWArgs.dialect_kwargs
.
-
method
sqlalchemy.sql.expression.Delete.
params(*arg, **kw)¶ inherited from the
UpdateBase.params()
method ofUpdateBase
Set the parameters for the statement.
This method raises
NotImplementedError
on the base class, and is overridden byValuesBase
to provide the SET/VALUES clause of UPDATE and INSERT.
-
method
sqlalchemy.sql.expression.Delete.
prefix_with(*expr, **kw)¶ inherited from the
HasPrefixes.prefix_with()
method ofHasPrefixes
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")
Multiple prefixes can be specified by multiple calls to
prefix_with()
.- Parameters:
*expr – textual or
ClauseElement
construct 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.Delete.
returning(*cols)¶ inherited from the
UpdateBase.returning()
method ofUpdateBase
Add a RETURNING or equivalent clause to this statement.
e.g.:
stmt = table.update().\ where(table.c.data == 'value').\ values(status='X').\ returning(table.c.server_flag, table.c.updated_timestamp) for server_flag, updated_timestamp in connection.execute(stmt): print(server_flag, updated_timestamp)
The given collection of column expressions should be derived from the table that is the target of the INSERT, UPDATE, or DELETE. While
Column
objects are typical, the elements can also be expressions:stmt = table.insert().returning( (table.c.first_name + " " + table.c.last_name). label('fullname'))
Upon compilation, a RETURNING clause, or database equivalent, will be rendered within the statement. For INSERT and UPDATE, the values are the newly inserted/updated values. For DELETE, the values are those of the rows which were deleted.
Upon execution, the values of the columns to be returned are made available via the result set and can be iterated using
ResultProxy.fetchone()
and similar. For DBAPIs which do not natively support returning values (i.e. cx_oracle), SQLAlchemy will approximate this behavior at the result level so that a reasonable amount of behavioral neutrality is provided.Note that not all databases/DBAPIs support RETURNING. For those backends with no support, an exception is raised upon compilation and/or execution. For those who do support it, the functionality across backends varies greatly, including restrictions on executemany() and other statements which return multiple rows. Please read the documentation notes for the database in use in order to determine the availability of RETURNING.
See also
ValuesBase.return_defaults()
- an alternative method tailored towards efficient fetching of server-side defaults and triggers for single-row INSERTs or UPDATEs.
-
method
sqlalchemy.sql.expression.Delete.
scalar(*multiparams, **params)¶ inherited from the
Executable.scalar()
method ofExecutable
Compile and execute this
Executable
, returning the result’s scalar representation.
-
method
sqlalchemy.sql.expression.Delete.
self_group(against=None)¶ inherited from the
ClauseElement.self_group()
method ofClauseElement
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 ofClauseElement
just returns self.
-
method
sqlalchemy.sql.expression.Delete.
unique_params(*optionaldict, **kwargs)¶ inherited from the
ClauseElement.unique_params()
method ofClauseElement
Return a copy with
bindparam()
elements replaced.Same functionality as
params()
, except adds unique=True to affected bind parameters so that multiple statements can be used.
-
method
sqlalchemy.sql.expression.Delete.
where(whereclause)¶ Add the given WHERE clause to a newly returned delete construct.
-
method
sqlalchemy.sql.expression.Delete.
with_hint(text, selectable=None, dialect_name='*')¶ inherited from the
UpdateBase.with_hint()
method ofUpdateBase
Add a table hint for a single table to this INSERT/UPDATE/DELETE statement.
Note
UpdateBase.with_hint()
currently applies only to Microsoft SQL Server. For MySQL INSERT/UPDATE/DELETE hints, useUpdateBase.prefix_with()
.The text of the hint is rendered in the appropriate location for the database backend in use, relative to the
Table
that is the subject of this statement, or optionally to that of the givenTable
passed as theselectable
argument.The
dialect_name
option will limit the rendering of a particular hint to a particular backend. Such as, to add a hint that only takes effect for SQL Server:mytable.insert().with_hint("WITH (PAGLOCK)", dialect_name="mssql")
- Parameters:
text – Text of the hint.
selectable – optional
Table
that specifies an element of the FROM clause within an UPDATE or DELETE to be the subject of the hint - applies only to certain backends.dialect_name – defaults to
*
, if specified as the name of a particular dialect, will apply these hints only when that dialect is in use.
-
method
- class sqlalchemy.sql.expression.Insert(table, values=None, inline=False, bind=None, prefixes=None, returning=None, return_defaults=False, **dialect_kw)¶
Represent an INSERT construct.
The
Insert
object is created using theinsert()
function.See also
Members
__init__(), argument_for(), bind, compare(), compile(), cte(), dialect_kwargs, dialect_options, execute(), execution_options(), from_select(), get_children(), kwargs, params(), prefix_with(), return_defaults(), returning(), scalar(), self_group(), unique_params(), values(), with_hint()
Class signature
class
sqlalchemy.sql.expression.Insert
(sqlalchemy.sql.expression.ValuesBase
)-
method
sqlalchemy.sql.expression.Insert.
__init__(table, values=None, inline=False, bind=None, prefixes=None, returning=None, return_defaults=False, **dialect_kw)¶ Construct a new
Insert
object.This constructor is mirrored as a public API function; see
insert()
for a full usage and argument description.
-
classmethod
sqlalchemy.sql.expression.Insert.
argument_for(dialect_name, argument_name, default)¶ inherited from the
DialectKWArgs.argument_for()
method ofDialectKWArgs
Add a new kind of dialect-specific keyword argument for this class.
E.g.:
Index.argument_for("mydialect", "length", None) some_index = Index('a', 'b', mydialect_length=5)
The
DialectKWArgs.argument_for()
method is a per-argument way adding extra arguments to theDefaultDialect.construct_arguments
dictionary. This dictionary provides a list of argument names accepted by various schema-level constructs on behalf of a dialect.New dialects should typically specify this dictionary all at once as a data member of the dialect class. The use case for ad-hoc addition of argument names is typically for end-user code that is also using a custom compilation scheme which consumes the additional arguments.
- Parameters:
dialect_name – name of a dialect. The dialect must be locatable, else a
NoSuchModuleError
is raised. The dialect must also include an existingDefaultDialect.construct_arguments
collection, indicating that it participates in the keyword-argument validation and default system, elseArgumentError
is raised. If the dialect does not include this collection, then any keyword argument can be specified on behalf of this dialect already. All dialects packaged within SQLAlchemy include this collection, however for third party dialects, support may vary.argument_name – name of the parameter.
default – default value of the parameter.
New in version 0.9.4.
-
attribute
sqlalchemy.sql.expression.Insert.
bind¶ inherited from the
UpdateBase.bind
attribute ofUpdateBase
Return a ‘bind’ linked to this
UpdateBase
or aTable
associated with it.
-
method
sqlalchemy.sql.expression.Insert.
compare(other, **kw)¶ inherited from the
ClauseElement.compare()
method ofClauseElement
Compare this ClauseElement to the given ClauseElement.
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. (see
ColumnElement
)
-
method
sqlalchemy.sql.expression.Insert.
compile(default, bind=None, dialect=None, **kw)¶ inherited from the
ClauseElement.compile()
method ofClauseElement
Compile this SQL expression.
The return value is a
Compiled
object. Callingstr()
orunicode()
on the returned value will yield a string representation of the result. TheCompiled
object also can return a dictionary of bind parameter names and values using theparams
accessor.- Parameters:
bind – An
Engine
orConnection
from which aCompiled
will 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
Dialect
instance from which aCompiled
will be acquired. This argument takes precedence over the bind argument as well as thisClauseElement
’s bound engine, if any.inline – Used for INSERT statements, for a dialect which does not support inline retrieval of newly generated primary key columns, will force the expression used to create the new primary key value to be rendered inline within the INSERT statement’s VALUES clause. This typically refers to Sequence execution but may also refer to any server-side default generation function associated with a primary key Column.
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_binds
flag 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.Insert.
cte(name=None, recursive=False)¶ inherited from the
HasCTE.cte()
method ofHasCTE
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.
SQLAlchemy detects
CTE
objects, which are treated similarly toAlias
objects, 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.Changed in version 1.1: Added support for UPDATE/INSERT/DELETE as CTE, CTEs added to UPDATE/INSERT/DELETE.
- Parameters:
name – name given to the common table expression. Like
_FromClause.alias()
, the name can be left asNone
in 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.
The following examples include two from PostgreSQL’s documentation at http://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)
See also
Query.cte()
- ORM version ofHasCTE.cte()
.
-
attribute
sqlalchemy.sql.expression.Insert.
dialect_kwargs¶ inherited from the
DialectKWArgs.dialect_kwargs
attribute ofDialectKWArgs
A collection of keyword arguments specified as dialect-specific options to this construct.
The arguments are present here in their original
<dialect>_<kwarg>
format. Only arguments that were actually passed are included; unlike theDialectKWArgs.dialect_options
collection, which contains all options known by this dialect including defaults.The collection is also writable; keys are accepted of the form
<dialect>_<kwarg>
where the value will be assembled into the list of options.New in version 0.9.2.
Changed in version 0.9.4: The
DialectKWArgs.dialect_kwargs
collection is now writable.See also
DialectKWArgs.dialect_options
- nested dictionary form
-
attribute
sqlalchemy.sql.expression.Insert.
dialect_options¶ inherited from the
DialectKWArgs.dialect_options
attribute ofDialectKWArgs
A collection of keyword arguments specified as dialect-specific options to this construct.
This is a two-level nested registry, keyed to
<dialect_name>
and<argument_name>
. For example, thepostgresql_where
argument would be locatable as:arg = my_object.dialect_options['postgresql']['where']
New in version 0.9.2.
See also
DialectKWArgs.dialect_kwargs
- flat dictionary form
-
method
sqlalchemy.sql.expression.Insert.
execute(*multiparams, **params)¶ inherited from the
Executable.execute()
method ofExecutable
Compile and execute this
Executable
.
-
method
sqlalchemy.sql.expression.Insert.
execution_options(**kw)¶ inherited from the
Executable.execution_options()
method ofExecutable
Set non-SQL options for the statement which take effect during execution.
Execution options can be set on a per-statement or per
Connection
basis. Additionally, theEngine
and ORMQuery
objects 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.Insert.
from_select(names, select, include_defaults=True)¶ Return a new
Insert
construct which represents anINSERT...FROM SELECT
statement.e.g.:
sel = select([table1.c.a, table1.c.b]).where(table1.c.c > 5) ins = table2.insert().from_select(['a', 'b'], sel)
- Parameters:
names – a sequence of string column names or
Column
objects representing the target columns.select – a
select()
construct,FromClause
or other construct which resolves into aFromClause
, such as an ORMQuery
object, etc. The order of columns returned from this FROM clause should correspond to the order of columns sent as thenames
parameter; while this is not checked before passing along to the database, the database would normally raise an exception if these column lists don’t correspond.include_defaults –
if True, non-server default values and SQL expressions as specified on
Column
objects (as documented in Column Insert/Update Defaults) not otherwise specified in the list of names will be rendered into the INSERT and SELECT statements, so that these values are also included in the data to be inserted.Note
A Python-side default that uses a Python callable function will only be invoked once for the whole statement, and not per row.
New in version 1.0.0: -
Insert.from_select()
now renders Python-side and SQL expression column defaults into the SELECT statement for columns otherwise not included in the list of column names.
Changed in version 1.0.0: an INSERT that uses FROM SELECT implies that the
insert.inline
flag is set to True, indicating that the statement will not attempt to fetch the “last inserted primary key” or other defaults. The statement deals with an arbitrary number of rows, so theResultProxy.inserted_primary_key
accessor does not apply.
-
method
sqlalchemy.sql.expression.Insert.
get_children(**kwargs)¶ Return immediate child elements of this
ClauseElement
.This is used for visit traversal.
**kwargs 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.Insert.
kwargs¶ inherited from the
DialectKWArgs.kwargs
attribute ofDialectKWArgs
A synonym for
DialectKWArgs.dialect_kwargs
.
-
method
sqlalchemy.sql.expression.Insert.
params(*arg, **kw)¶ inherited from the
UpdateBase.params()
method ofUpdateBase
Set the parameters for the statement.
This method raises
NotImplementedError
on the base class, and is overridden byValuesBase
to provide the SET/VALUES clause of UPDATE and INSERT.
-
method
sqlalchemy.sql.expression.Insert.
prefix_with(*expr, **kw)¶ inherited from the
HasPrefixes.prefix_with()
method ofHasPrefixes
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")
Multiple prefixes can be specified by multiple calls to
prefix_with()
.- Parameters:
*expr – textual or
ClauseElement
construct 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.Insert.
return_defaults(*cols)¶ inherited from the
ValuesBase.return_defaults()
method ofValuesBase
Make use of a RETURNING clause for the purpose of fetching server-side expressions and defaults.
E.g.:
stmt = table.insert().values(data='newdata').return_defaults() result = connection.execute(stmt) server_created_at = result.returned_defaults['created_at']
When used against a backend that supports RETURNING, all column values generated by SQL expression or server-side-default will be added to any existing RETURNING clause, provided that
UpdateBase.returning()
is not used simultaneously. The column values will then be available on the result using theResultProxy.returned_defaults
accessor as a dictionary, referring to values keyed to theColumn
object as well as its.key
.This method differs from
UpdateBase.returning()
in these ways:ValuesBase.return_defaults()
is only intended for use with an INSERT or an UPDATE statement that matches exactly one row. While the RETURNING construct in the general sense supports multiple rows for a multi-row UPDATE or DELETE statement, or for special cases of INSERT that return multiple rows (e.g. INSERT from SELECT, multi-valued VALUES clause),ValuesBase.return_defaults()
is intended only for an “ORM-style” single-row INSERT/UPDATE statement. The row returned by the statement is also consumed implicitly whenValuesBase.return_defaults()
is used. By contrast,UpdateBase.returning()
leaves the RETURNING result-set intact with a collection of any number of rows.It is compatible with the existing logic to fetch auto-generated primary key values, also known as “implicit returning”. Backends that support RETURNING will automatically make use of RETURNING in order to fetch the value of newly generated primary keys; while the
UpdateBase.returning()
method circumvents this behavior,ValuesBase.return_defaults()
leaves it intact.It can be called against any backend. Backends that don’t support RETURNING will skip the usage of the feature, rather than raising an exception. The return value of
ResultProxy.returned_defaults
will beNone
ValuesBase.return_defaults()
is used by the ORM to provide an efficient implementation for theeager_defaults
feature ofmapper()
.- Parameters:
cols – optional list of column key names or
Column
objects. If omitted, all column expressions evaluated on the server are added to the returning list.
New in version 0.9.0.
-
method
sqlalchemy.sql.expression.Insert.
returning(*cols)¶ inherited from the
UpdateBase.returning()
method ofUpdateBase
Add a RETURNING or equivalent clause to this statement.
e.g.:
stmt = table.update().\ where(table.c.data == 'value').\ values(status='X').\ returning(table.c.server_flag, table.c.updated_timestamp) for server_flag, updated_timestamp in connection.execute(stmt): print(server_flag, updated_timestamp)
The given collection of column expressions should be derived from the table that is the target of the INSERT, UPDATE, or DELETE. While
Column
objects are typical, the elements can also be expressions:stmt = table.insert().returning( (table.c.first_name + " " + table.c.last_name). label('fullname'))
Upon compilation, a RETURNING clause, or database equivalent, will be rendered within the statement. For INSERT and UPDATE, the values are the newly inserted/updated values. For DELETE, the values are those of the rows which were deleted.
Upon execution, the values of the columns to be returned are made available via the result set and can be iterated using
ResultProxy.fetchone()
and similar. For DBAPIs which do not natively support returning values (i.e. cx_oracle), SQLAlchemy will approximate this behavior at the result level so that a reasonable amount of behavioral neutrality is provided.Note that not all databases/DBAPIs support RETURNING. For those backends with no support, an exception is raised upon compilation and/or execution. For those who do support it, the functionality across backends varies greatly, including restrictions on executemany() and other statements which return multiple rows. Please read the documentation notes for the database in use in order to determine the availability of RETURNING.
See also
ValuesBase.return_defaults()
- an alternative method tailored towards efficient fetching of server-side defaults and triggers for single-row INSERTs or UPDATEs.
-
method
sqlalchemy.sql.expression.Insert.
scalar(*multiparams, **params)¶ inherited from the
Executable.scalar()
method ofExecutable
Compile and execute this
Executable
, returning the result’s scalar representation.
-
method
sqlalchemy.sql.expression.Insert.
self_group(against=None)¶ inherited from the
ClauseElement.self_group()
method ofClauseElement
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 ofClauseElement
just returns self.
-
method
sqlalchemy.sql.expression.Insert.
unique_params(*optionaldict, **kwargs)¶ inherited from the
ClauseElement.unique_params()
method ofClauseElement
Return a copy with
bindparam()
elements replaced.Same functionality as
params()
, except adds unique=True to affected bind parameters so that multiple statements can be used.
-
method
sqlalchemy.sql.expression.Insert.
values(*args, **kwargs)¶ inherited from the
ValuesBase.values()
method ofValuesBase
specify a fixed VALUES clause for an INSERT statement, or the SET clause for an UPDATE.
Note that the
Insert
andUpdate
constructs support per-execution time formatting of the VALUES and/or SET clauses, based on the arguments passed toConnection.execute()
. However, theValuesBase.values()
method can be used to “fix” a particular set of parameters into the statement.Multiple calls to
ValuesBase.values()
will produce a new construct, each one with the parameter list modified to include the new parameters sent. In the typical case of a single dictionary of parameters, the newly passed keys will replace the same keys in the previous construct. In the case of a list-based “multiple values” construct, each new list of values is extended onto the existing list of values.- Parameters:
**kwargs –
key value pairs representing the string key of a
Column
mapped to the value to be rendered into the VALUES or SET clause:users.insert().values(name="some name") users.update().where(users.c.id==5).values(name="some name")
*args –
As an alternative to passing key/value parameters, a dictionary, tuple, or list of dictionaries or tuples can be passed as a single positional argument in order to form the VALUES or SET clause of the statement. The forms that are accepted vary based on whether this is an
Insert
or anUpdate
construct.For either an
Insert
orUpdate
construct, a single dictionary can be passed, which works the same as that of the kwargs form:users.insert().values({"name": "some name"}) users.update().values({"name": "some new name"})
Also for either form but more typically for the
Insert
construct, a tuple that contains an entry for every column in the table is also accepted:users.insert().values((5, "some name"))
The
Insert
construct also supports being passed a list of dictionaries or full-table-tuples, which on the server will render the less common SQL syntax of “multiple values” - this syntax is supported on backends such as SQLite, PostgreSQL, MySQL, but not necessarily others:users.insert().values([ {"name": "some name"}, {"name": "some other name"}, {"name": "yet another name"}, ])
The above form would render a multiple VALUES statement similar to:
INSERT INTO users (name) VALUES (:name_1), (:name_2), (:name_3)
It is essential to note that passing multiple values is NOT the same as using traditional executemany() form. The above syntax is a special syntax not typically used. To emit an INSERT statement against multiple rows, the normal method is to pass a multiple values list to the
Connection.execute()
method, which is supported by all database backends and is generally more efficient for a very large number of parameters.See also
Executing Multiple Statements - an introduction to the traditional Core method of multiple parameter set invocation for INSERTs and other statements.
Changed in version 1.0.0: an INSERT that uses a multiple-VALUES clause, even a list of length one, implies that the
Insert.inline
flag is set to True, indicating that the statement will not attempt to fetch the “last inserted primary key” or other defaults. The statement deals with an arbitrary number of rows, so theResultProxy.inserted_primary_key
accessor does not apply.Changed in version 1.0.0: A multiple-VALUES INSERT now supports columns with Python side default values and callables in the same way as that of an “executemany” style of invocation; the callable is invoked for each row. See Python-side defaults invoked for each row individually when using a multivalued insert for other details.
The
Update
construct supports a special form which is a list of 2-tuples, which when provided must be passed in conjunction with theupdate.preserve_parameter_order
parameter. This form causes the UPDATE statement to render the SET clauses using the order of parameters given toUpdate.values()
, rather than the ordering of columns given in theTable
.New in version 1.0.10: - added support for parameter-ordered UPDATE statements via the
update.preserve_parameter_order
flag.See also
Parameter-Ordered Updates - full example of the
update.preserve_parameter_order
flag
See also
Inserts, Updates and Deletes - SQL Expression Language Tutorial
insert()
- produce anINSERT
statementupdate()
- produce anUPDATE
statement
-
method
sqlalchemy.sql.expression.Insert.
with_hint(text, selectable=None, dialect_name='*')¶ inherited from the
UpdateBase.with_hint()
method ofUpdateBase
Add a table hint for a single table to this INSERT/UPDATE/DELETE statement.
Note
UpdateBase.with_hint()
currently applies only to Microsoft SQL Server. For MySQL INSERT/UPDATE/DELETE hints, useUpdateBase.prefix_with()
.The text of the hint is rendered in the appropriate location for the database backend in use, relative to the
Table
that is the subject of this statement, or optionally to that of the givenTable
passed as theselectable
argument.The
dialect_name
option will limit the rendering of a particular hint to a particular backend. Such as, to add a hint that only takes effect for SQL Server:mytable.insert().with_hint("WITH (PAGLOCK)", dialect_name="mssql")
- Parameters:
text – Text of the hint.
selectable – optional
Table
that specifies an element of the FROM clause within an UPDATE or DELETE to be the subject of the hint - applies only to certain backends.dialect_name – defaults to
*
, if specified as the name of a particular dialect, will apply these hints only when that dialect is in use.
-
method
- class sqlalchemy.sql.expression.Update(table, whereclause=None, values=None, inline=False, bind=None, prefixes=None, returning=None, return_defaults=False, preserve_parameter_order=False, **dialect_kw)¶
Represent an Update construct.
The
Update
object is created using theupdate()
function.Members
__init__(), argument_for(), bind, compare(), compile(), cte(), dialect_kwargs, dialect_options, execute(), execution_options(), get_children(), kwargs, params(), prefix_with(), return_defaults(), returning(), scalar(), self_group(), unique_params(), values(), where(), with_hint()
Class signature
class
sqlalchemy.sql.expression.Update
(sqlalchemy.sql.expression.ValuesBase
)-
method
sqlalchemy.sql.expression.Update.
__init__(table, whereclause=None, values=None, inline=False, bind=None, prefixes=None, returning=None, return_defaults=False, preserve_parameter_order=False, **dialect_kw)¶ Construct a new
Update
object.This constructor is mirrored as a public API function; see
update()
for a full usage and argument description.
-
classmethod
sqlalchemy.sql.expression.Update.
argument_for(dialect_name, argument_name, default)¶ inherited from the
DialectKWArgs.argument_for()
method ofDialectKWArgs
Add a new kind of dialect-specific keyword argument for this class.
E.g.:
Index.argument_for("mydialect", "length", None) some_index = Index('a', 'b', mydialect_length=5)
The
DialectKWArgs.argument_for()
method is a per-argument way adding extra arguments to theDefaultDialect.construct_arguments
dictionary. This dictionary provides a list of argument names accepted by various schema-level constructs on behalf of a dialect.New dialects should typically specify this dictionary all at once as a data member of the dialect class. The use case for ad-hoc addition of argument names is typically for end-user code that is also using a custom compilation scheme which consumes the additional arguments.
- Parameters:
dialect_name – name of a dialect. The dialect must be locatable, else a
NoSuchModuleError
is raised. The dialect must also include an existingDefaultDialect.construct_arguments
collection, indicating that it participates in the keyword-argument validation and default system, elseArgumentError
is raised. If the dialect does not include this collection, then any keyword argument can be specified on behalf of this dialect already. All dialects packaged within SQLAlchemy include this collection, however for third party dialects, support may vary.argument_name – name of the parameter.
default – default value of the parameter.
New in version 0.9.4.
-
attribute
sqlalchemy.sql.expression.Update.
bind¶ inherited from the
UpdateBase.bind
attribute ofUpdateBase
Return a ‘bind’ linked to this
UpdateBase
or aTable
associated with it.
-
method
sqlalchemy.sql.expression.Update.
compare(other, **kw)¶ inherited from the
ClauseElement.compare()
method ofClauseElement
Compare this ClauseElement to the given ClauseElement.
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. (see
ColumnElement
)
-
method
sqlalchemy.sql.expression.Update.
compile(default, bind=None, dialect=None, **kw)¶ inherited from the
ClauseElement.compile()
method ofClauseElement
Compile this SQL expression.
The return value is a
Compiled
object. Callingstr()
orunicode()
on the returned value will yield a string representation of the result. TheCompiled
object also can return a dictionary of bind parameter names and values using theparams
accessor.- Parameters:
bind – An
Engine
orConnection
from which aCompiled
will 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
Dialect
instance from which aCompiled
will be acquired. This argument takes precedence over the bind argument as well as thisClauseElement
’s bound engine, if any.inline – Used for INSERT statements, for a dialect which does not support inline retrieval of newly generated primary key columns, will force the expression used to create the new primary key value to be rendered inline within the INSERT statement’s VALUES clause. This typically refers to Sequence execution but may also refer to any server-side default generation function associated with a primary key Column.
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_binds
flag 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.Update.
cte(name=None, recursive=False)¶ inherited from the
HasCTE.cte()
method ofHasCTE
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.
SQLAlchemy detects
CTE
objects, which are treated similarly toAlias
objects, 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.Changed in version 1.1: Added support for UPDATE/INSERT/DELETE as CTE, CTEs added to UPDATE/INSERT/DELETE.
- Parameters:
name – name given to the common table expression. Like
_FromClause.alias()
, the name can be left asNone
in 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.
The following examples include two from PostgreSQL’s documentation at http://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)
See also
Query.cte()
- ORM version ofHasCTE.cte()
.
-
attribute
sqlalchemy.sql.expression.Update.
dialect_kwargs¶ inherited from the
DialectKWArgs.dialect_kwargs
attribute ofDialectKWArgs
A collection of keyword arguments specified as dialect-specific options to this construct.
The arguments are present here in their original
<dialect>_<kwarg>
format. Only arguments that were actually passed are included; unlike theDialectKWArgs.dialect_options
collection, which contains all options known by this dialect including defaults.The collection is also writable; keys are accepted of the form
<dialect>_<kwarg>
where the value will be assembled into the list of options.New in version 0.9.2.
Changed in version 0.9.4: The
DialectKWArgs.dialect_kwargs
collection is now writable.See also
DialectKWArgs.dialect_options
- nested dictionary form
-
attribute
sqlalchemy.sql.expression.Update.
dialect_options¶ inherited from the
DialectKWArgs.dialect_options
attribute ofDialectKWArgs
A collection of keyword arguments specified as dialect-specific options to this construct.
This is a two-level nested registry, keyed to
<dialect_name>
and<argument_name>
. For example, thepostgresql_where
argument would be locatable as:arg = my_object.dialect_options['postgresql']['where']
New in version 0.9.2.
See also
DialectKWArgs.dialect_kwargs
- flat dictionary form
-
method
sqlalchemy.sql.expression.Update.
execute(*multiparams, **params)¶ inherited from the
Executable.execute()
method ofExecutable
Compile and execute this
Executable
.
-
method
sqlalchemy.sql.expression.Update.
execution_options(**kw)¶ inherited from the
Executable.execution_options()
method ofExecutable
Set non-SQL options for the statement which take effect during execution.
Execution options can be set on a per-statement or per
Connection
basis. Additionally, theEngine
and ORMQuery
objects 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.Update.
get_children(**kwargs)¶ Return immediate child elements of this
ClauseElement
.This is used for visit traversal.
**kwargs 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.Update.
kwargs¶ inherited from the
DialectKWArgs.kwargs
attribute ofDialectKWArgs
A synonym for
DialectKWArgs.dialect_kwargs
.
-
method
sqlalchemy.sql.expression.Update.
params(*arg, **kw)¶ inherited from the
UpdateBase.params()
method ofUpdateBase
Set the parameters for the statement.
This method raises
NotImplementedError
on the base class, and is overridden byValuesBase
to provide the SET/VALUES clause of UPDATE and INSERT.
-
method
sqlalchemy.sql.expression.Update.
prefix_with(*expr, **kw)¶ inherited from the
HasPrefixes.prefix_with()
method ofHasPrefixes
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")
Multiple prefixes can be specified by multiple calls to
prefix_with()
.- Parameters:
*expr – textual or
ClauseElement
construct 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.Update.
return_defaults(*cols)¶ inherited from the
ValuesBase.return_defaults()
method ofValuesBase
Make use of a RETURNING clause for the purpose of fetching server-side expressions and defaults.
E.g.:
stmt = table.insert().values(data='newdata').return_defaults() result = connection.execute(stmt) server_created_at = result.returned_defaults['created_at']
When used against a backend that supports RETURNING, all column values generated by SQL expression or server-side-default will be added to any existing RETURNING clause, provided that
UpdateBase.returning()
is not used simultaneously. The column values will then be available on the result using theResultProxy.returned_defaults
accessor as a dictionary, referring to values keyed to theColumn
object as well as its.key
.This method differs from
UpdateBase.returning()
in these ways:ValuesBase.return_defaults()
is only intended for use with an INSERT or an UPDATE statement that matches exactly one row. While the RETURNING construct in the general sense supports multiple rows for a multi-row UPDATE or DELETE statement, or for special cases of INSERT that return multiple rows (e.g. INSERT from SELECT, multi-valued VALUES clause),ValuesBase.return_defaults()
is intended only for an “ORM-style” single-row INSERT/UPDATE statement. The row returned by the statement is also consumed implicitly whenValuesBase.return_defaults()
is used. By contrast,UpdateBase.returning()
leaves the RETURNING result-set intact with a collection of any number of rows.It is compatible with the existing logic to fetch auto-generated primary key values, also known as “implicit returning”. Backends that support RETURNING will automatically make use of RETURNING in order to fetch the value of newly generated primary keys; while the
UpdateBase.returning()
method circumvents this behavior,ValuesBase.return_defaults()
leaves it intact.It can be called against any backend. Backends that don’t support RETURNING will skip the usage of the feature, rather than raising an exception. The return value of
ResultProxy.returned_defaults
will beNone
ValuesBase.return_defaults()
is used by the ORM to provide an efficient implementation for theeager_defaults
feature ofmapper()
.- Parameters:
cols – optional list of column key names or
Column
objects. If omitted, all column expressions evaluated on the server are added to the returning list.
New in version 0.9.0.
-
method
sqlalchemy.sql.expression.Update.
returning(*cols)¶ inherited from the
UpdateBase.returning()
method ofUpdateBase
Add a RETURNING or equivalent clause to this statement.
e.g.:
stmt = table.update().\ where(table.c.data == 'value').\ values(status='X').\ returning(table.c.server_flag, table.c.updated_timestamp) for server_flag, updated_timestamp in connection.execute(stmt): print(server_flag, updated_timestamp)
The given collection of column expressions should be derived from the table that is the target of the INSERT, UPDATE, or DELETE. While
Column
objects are typical, the elements can also be expressions:stmt = table.insert().returning( (table.c.first_name + " " + table.c.last_name). label('fullname'))
Upon compilation, a RETURNING clause, or database equivalent, will be rendered within the statement. For INSERT and UPDATE, the values are the newly inserted/updated values. For DELETE, the values are those of the rows which were deleted.
Upon execution, the values of the columns to be returned are made available via the result set and can be iterated using
ResultProxy.fetchone()
and similar. For DBAPIs which do not natively support returning values (i.e. cx_oracle), SQLAlchemy will approximate this behavior at the result level so that a reasonable amount of behavioral neutrality is provided.Note that not all databases/DBAPIs support RETURNING. For those backends with no support, an exception is raised upon compilation and/or execution. For those who do support it, the functionality across backends varies greatly, including restrictions on executemany() and other statements which return multiple rows. Please read the documentation notes for the database in use in order to determine the availability of RETURNING.
See also
ValuesBase.return_defaults()
- an alternative method tailored towards efficient fetching of server-side defaults and triggers for single-row INSERTs or UPDATEs.
-
method
sqlalchemy.sql.expression.Update.
scalar(*multiparams, **params)¶ inherited from the
Executable.scalar()
method ofExecutable
Compile and execute this
Executable
, returning the result’s scalar representation.
-
method
sqlalchemy.sql.expression.Update.
self_group(against=None)¶ inherited from the
ClauseElement.self_group()
method ofClauseElement
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 ofClauseElement
just returns self.
-
method
sqlalchemy.sql.expression.Update.
unique_params(*optionaldict, **kwargs)¶ inherited from the
ClauseElement.unique_params()
method ofClauseElement
Return a copy with
bindparam()
elements replaced.Same functionality as
params()
, except adds unique=True to affected bind parameters so that multiple statements can be used.
-
method
sqlalchemy.sql.expression.Update.
values(*args, **kwargs)¶ inherited from the
ValuesBase.values()
method ofValuesBase
specify a fixed VALUES clause for an INSERT statement, or the SET clause for an UPDATE.
Note that the
Insert
andUpdate
constructs support per-execution time formatting of the VALUES and/or SET clauses, based on the arguments passed toConnection.execute()
. However, theValuesBase.values()
method can be used to “fix” a particular set of parameters into the statement.Multiple calls to
ValuesBase.values()
will produce a new construct, each one with the parameter list modified to include the new parameters sent. In the typical case of a single dictionary of parameters, the newly passed keys will replace the same keys in the previous construct. In the case of a list-based “multiple values” construct, each new list of values is extended onto the existing list of values.- Parameters:
**kwargs –
key value pairs representing the string key of a
Column
mapped to the value to be rendered into the VALUES or SET clause:users.insert().values(name="some name") users.update().where(users.c.id==5).values(name="some name")
*args –
As an alternative to passing key/value parameters, a dictionary, tuple, or list of dictionaries or tuples can be passed as a single positional argument in order to form the VALUES or SET clause of the statement. The forms that are accepted vary based on whether this is an
Insert
or anUpdate
construct.For either an
Insert
orUpdate
construct, a single dictionary can be passed, which works the same as that of the kwargs form:users.insert().values({"name": "some name"}) users.update().values({"name": "some new name"})
Also for either form but more typically for the
Insert
construct, a tuple that contains an entry for every column in the table is also accepted:users.insert().values((5, "some name"))
The
Insert
construct also supports being passed a list of dictionaries or full-table-tuples, which on the server will render the less common SQL syntax of “multiple values” - this syntax is supported on backends such as SQLite, PostgreSQL, MySQL, but not necessarily others:users.insert().values([ {"name": "some name"}, {"name": "some other name"}, {"name": "yet another name"}, ])
The above form would render a multiple VALUES statement similar to:
INSERT INTO users (name) VALUES (:name_1), (:name_2), (:name_3)
It is essential to note that passing multiple values is NOT the same as using traditional executemany() form. The above syntax is a special syntax not typically used. To emit an INSERT statement against multiple rows, the normal method is to pass a multiple values list to the
Connection.execute()
method, which is supported by all database backends and is generally more efficient for a very large number of parameters.See also
Executing Multiple Statements - an introduction to the traditional Core method of multiple parameter set invocation for INSERTs and other statements.
Changed in version 1.0.0: an INSERT that uses a multiple-VALUES clause, even a list of length one, implies that the
Insert.inline
flag is set to True, indicating that the statement will not attempt to fetch the “last inserted primary key” or other defaults. The statement deals with an arbitrary number of rows, so theResultProxy.inserted_primary_key
accessor does not apply.Changed in version 1.0.0: A multiple-VALUES INSERT now supports columns with Python side default values and callables in the same way as that of an “executemany” style of invocation; the callable is invoked for each row. See Python-side defaults invoked for each row individually when using a multivalued insert for other details.
The
Update
construct supports a special form which is a list of 2-tuples, which when provided must be passed in conjunction with theupdate.preserve_parameter_order
parameter. This form causes the UPDATE statement to render the SET clauses using the order of parameters given toUpdate.values()
, rather than the ordering of columns given in theTable
.New in version 1.0.10: - added support for parameter-ordered UPDATE statements via the
update.preserve_parameter_order
flag.See also
Parameter-Ordered Updates - full example of the
update.preserve_parameter_order
flag
See also
Inserts, Updates and Deletes - SQL Expression Language Tutorial
insert()
- produce anINSERT
statementupdate()
- produce anUPDATE
statement
-
method
sqlalchemy.sql.expression.Update.
where(whereclause)¶ return a new update() construct with the given expression added to its WHERE clause, joined to the existing clause via AND, if any.
-
method
sqlalchemy.sql.expression.Update.
with_hint(text, selectable=None, dialect_name='*')¶ inherited from the
UpdateBase.with_hint()
method ofUpdateBase
Add a table hint for a single table to this INSERT/UPDATE/DELETE statement.
Note
UpdateBase.with_hint()
currently applies only to Microsoft SQL Server. For MySQL INSERT/UPDATE/DELETE hints, useUpdateBase.prefix_with()
.The text of the hint is rendered in the appropriate location for the database backend in use, relative to the
Table
that is the subject of this statement, or optionally to that of the givenTable
passed as theselectable
argument.The
dialect_name
option will limit the rendering of a particular hint to a particular backend. Such as, to add a hint that only takes effect for SQL Server:mytable.insert().with_hint("WITH (PAGLOCK)", dialect_name="mssql")
- Parameters:
text – Text of the hint.
selectable – optional
Table
that specifies an element of the FROM clause within an UPDATE or DELETE to be the subject of the hint - applies only to certain backends.dialect_name – defaults to
*
, if specified as the name of a particular dialect, will apply these hints only when that dialect is in use.
-
method
- class sqlalchemy.sql.expression.UpdateBase¶
Form the base for
INSERT
,UPDATE
, andDELETE
statements.Members
argument_for(), bind, compare(), compile(), cte(), dialect_kwargs, dialect_options, execute(), execution_options(), get_children(), kwargs, params(), prefix_with(), returning(), scalar(), self_group(), unique_params(), with_hint()
Class signature
class
sqlalchemy.sql.expression.UpdateBase
(sqlalchemy.sql.expression.HasCTE
,sqlalchemy.sql.base.DialectKWArgs
,sqlalchemy.sql.expression.HasPrefixes
,sqlalchemy.sql.expression.Executable
,sqlalchemy.sql.expression.ClauseElement
)-
classmethod
sqlalchemy.sql.expression.UpdateBase.
argument_for(dialect_name, argument_name, default)¶ inherited from the
DialectKWArgs.argument_for()
method ofDialectKWArgs
Add a new kind of dialect-specific keyword argument for this class.
E.g.:
Index.argument_for("mydialect", "length", None) some_index = Index('a', 'b', mydialect_length=5)
The
DialectKWArgs.argument_for()
method is a per-argument way adding extra arguments to theDefaultDialect.construct_arguments
dictionary. This dictionary provides a list of argument names accepted by various schema-level constructs on behalf of a dialect.New dialects should typically specify this dictionary all at once as a data member of the dialect class. The use case for ad-hoc addition of argument names is typically for end-user code that is also using a custom compilation scheme which consumes the additional arguments.
- Parameters:
dialect_name – name of a dialect. The dialect must be locatable, else a
NoSuchModuleError
is raised. The dialect must also include an existingDefaultDialect.construct_arguments
collection, indicating that it participates in the keyword-argument validation and default system, elseArgumentError
is raised. If the dialect does not include this collection, then any keyword argument can be specified on behalf of this dialect already. All dialects packaged within SQLAlchemy include this collection, however for third party dialects, support may vary.argument_name – name of the parameter.
default – default value of the parameter.
New in version 0.9.4.
-
attribute
sqlalchemy.sql.expression.UpdateBase.
bind¶ Return a ‘bind’ linked to this
UpdateBase
or aTable
associated with it.
-
method
sqlalchemy.sql.expression.UpdateBase.
compare(other, **kw)¶ inherited from the
ClauseElement.compare()
method ofClauseElement
Compare this ClauseElement to the given ClauseElement.
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. (see
ColumnElement
)
-
method
sqlalchemy.sql.expression.UpdateBase.
compile(default, bind=None, dialect=None, **kw)¶ inherited from the
ClauseElement.compile()
method ofClauseElement
Compile this SQL expression.
The return value is a
Compiled
object. Callingstr()
orunicode()
on the returned value will yield a string representation of the result. TheCompiled
object also can return a dictionary of bind parameter names and values using theparams
accessor.- Parameters:
bind – An
Engine
orConnection
from which aCompiled
will 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
Dialect
instance from which aCompiled
will be acquired. This argument takes precedence over the bind argument as well as thisClauseElement
’s bound engine, if any.inline – Used for INSERT statements, for a dialect which does not support inline retrieval of newly generated primary key columns, will force the expression used to create the new primary key value to be rendered inline within the INSERT statement’s VALUES clause. This typically refers to Sequence execution but may also refer to any server-side default generation function associated with a primary key Column.
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_binds
flag 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.UpdateBase.
cte(name=None, recursive=False)¶ inherited from the
HasCTE.cte()
method ofHasCTE
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.
SQLAlchemy detects
CTE
objects, which are treated similarly toAlias
objects, 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.Changed in version 1.1: Added support for UPDATE/INSERT/DELETE as CTE, CTEs added to UPDATE/INSERT/DELETE.
- Parameters:
name – name given to the common table expression. Like
_FromClause.alias()
, the name can be left asNone
in 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.
The following examples include two from PostgreSQL’s documentation at http://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)
See also
Query.cte()
- ORM version ofHasCTE.cte()
.
-
attribute
sqlalchemy.sql.expression.UpdateBase.
dialect_kwargs¶ inherited from the
DialectKWArgs.dialect_kwargs
attribute ofDialectKWArgs
A collection of keyword arguments specified as dialect-specific options to this construct.
The arguments are present here in their original
<dialect>_<kwarg>
format. Only arguments that were actually passed are included; unlike theDialectKWArgs.dialect_options
collection, which contains all options known by this dialect including defaults.The collection is also writable; keys are accepted of the form
<dialect>_<kwarg>
where the value will be assembled into the list of options.New in version 0.9.2.
Changed in version 0.9.4: The
DialectKWArgs.dialect_kwargs
collection is now writable.See also
DialectKWArgs.dialect_options
- nested dictionary form
-
attribute
sqlalchemy.sql.expression.UpdateBase.
dialect_options¶ inherited from the
DialectKWArgs.dialect_options
attribute ofDialectKWArgs
A collection of keyword arguments specified as dialect-specific options to this construct.
This is a two-level nested registry, keyed to
<dialect_name>
and<argument_name>
. For example, thepostgresql_where
argument would be locatable as:arg = my_object.dialect_options['postgresql']['where']
New in version 0.9.2.
See also
DialectKWArgs.dialect_kwargs
- flat dictionary form
-
method
sqlalchemy.sql.expression.UpdateBase.
execute(*multiparams, **params)¶ inherited from the
Executable.execute()
method ofExecutable
Compile and execute this
Executable
.
-
method
sqlalchemy.sql.expression.UpdateBase.
execution_options(**kw)¶ inherited from the
Executable.execution_options()
method ofExecutable
Set non-SQL options for the statement which take effect during execution.
Execution options can be set on a per-statement or per
Connection
basis. Additionally, theEngine
and ORMQuery
objects 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.UpdateBase.
get_children(**kwargs)¶ inherited from the
ClauseElement.get_children()
method ofClauseElement
Return immediate child elements of this
ClauseElement
.This is used for visit traversal.
**kwargs 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.UpdateBase.
kwargs¶ inherited from the
DialectKWArgs.kwargs
attribute ofDialectKWArgs
A synonym for
DialectKWArgs.dialect_kwargs
.
-
method
sqlalchemy.sql.expression.UpdateBase.
params(*arg, **kw)¶ Set the parameters for the statement.
This method raises
NotImplementedError
on the base class, and is overridden byValuesBase
to provide the SET/VALUES clause of UPDATE and INSERT.
-
method
sqlalchemy.sql.expression.UpdateBase.
prefix_with(*expr, **kw)¶ inherited from the
HasPrefixes.prefix_with()
method ofHasPrefixes
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")
Multiple prefixes can be specified by multiple calls to
prefix_with()
.- Parameters:
*expr – textual or
ClauseElement
construct 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.UpdateBase.
returning(*cols)¶ Add a RETURNING or equivalent clause to this statement.
e.g.:
stmt = table.update().\ where(table.c.data == 'value').\ values(status='X').\ returning(table.c.server_flag, table.c.updated_timestamp) for server_flag, updated_timestamp in connection.execute(stmt): print(server_flag, updated_timestamp)
The given collection of column expressions should be derived from the table that is the target of the INSERT, UPDATE, or DELETE. While
Column
objects are typical, the elements can also be expressions:stmt = table.insert().returning( (table.c.first_name + " " + table.c.last_name). label('fullname'))
Upon compilation, a RETURNING clause, or database equivalent, will be rendered within the statement. For INSERT and UPDATE, the values are the newly inserted/updated values. For DELETE, the values are those of the rows which were deleted.
Upon execution, the values of the columns to be returned are made available via the result set and can be iterated using
ResultProxy.fetchone()
and similar. For DBAPIs which do not natively support returning values (i.e. cx_oracle), SQLAlchemy will approximate this behavior at the result level so that a reasonable amount of behavioral neutrality is provided.Note that not all databases/DBAPIs support RETURNING. For those backends with no support, an exception is raised upon compilation and/or execution. For those who do support it, the functionality across backends varies greatly, including restrictions on executemany() and other statements which return multiple rows. Please read the documentation notes for the database in use in order to determine the availability of RETURNING.
See also
ValuesBase.return_defaults()
- an alternative method tailored towards efficient fetching of server-side defaults and triggers for single-row INSERTs or UPDATEs.
-
method
sqlalchemy.sql.expression.UpdateBase.
scalar(*multiparams, **params)¶ inherited from the
Executable.scalar()
method ofExecutable
Compile and execute this
Executable
, returning the result’s scalar representation.
-
method
sqlalchemy.sql.expression.UpdateBase.
self_group(against=None)¶ inherited from the
ClauseElement.self_group()
method ofClauseElement
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 ofClauseElement
just returns self.
-
method
sqlalchemy.sql.expression.UpdateBase.
unique_params(*optionaldict, **kwargs)¶ inherited from the
ClauseElement.unique_params()
method ofClauseElement
Return a copy with
bindparam()
elements replaced.Same functionality as
params()
, except adds unique=True to affected bind parameters so that multiple statements can be used.
-
method
sqlalchemy.sql.expression.UpdateBase.
with_hint(text, selectable=None, dialect_name='*')¶ Add a table hint for a single table to this INSERT/UPDATE/DELETE statement.
Note
UpdateBase.with_hint()
currently applies only to Microsoft SQL Server. For MySQL INSERT/UPDATE/DELETE hints, useUpdateBase.prefix_with()
.The text of the hint is rendered in the appropriate location for the database backend in use, relative to the
Table
that is the subject of this statement, or optionally to that of the givenTable
passed as theselectable
argument.The
dialect_name
option will limit the rendering of a particular hint to a particular backend. Such as, to add a hint that only takes effect for SQL Server:mytable.insert().with_hint("WITH (PAGLOCK)", dialect_name="mssql")
- Parameters:
text – Text of the hint.
selectable – optional
Table
that specifies an element of the FROM clause within an UPDATE or DELETE to be the subject of the hint - applies only to certain backends.dialect_name – defaults to
*
, if specified as the name of a particular dialect, will apply these hints only when that dialect is in use.
-
classmethod
- class sqlalchemy.sql.expression.ValuesBase(table, values, prefixes)¶
Supplies support for
ValuesBase.values()
to INSERT and UPDATE constructs.Members
Class signature
class
sqlalchemy.sql.expression.ValuesBase
(sqlalchemy.sql.expression.UpdateBase
)-
method
sqlalchemy.sql.expression.ValuesBase.
return_defaults(*cols)¶ Make use of a RETURNING clause for the purpose of fetching server-side expressions and defaults.
E.g.:
stmt = table.insert().values(data='newdata').return_defaults() result = connection.execute(stmt) server_created_at = result.returned_defaults['created_at']
When used against a backend that supports RETURNING, all column values generated by SQL expression or server-side-default will be added to any existing RETURNING clause, provided that
UpdateBase.returning()
is not used simultaneously. The column values will then be available on the result using theResultProxy.returned_defaults
accessor as a dictionary, referring to values keyed to theColumn
object as well as its.key
.This method differs from
UpdateBase.returning()
in these ways:ValuesBase.return_defaults()
is only intended for use with an INSERT or an UPDATE statement that matches exactly one row. While the RETURNING construct in the general sense supports multiple rows for a multi-row UPDATE or DELETE statement, or for special cases of INSERT that return multiple rows (e.g. INSERT from SELECT, multi-valued VALUES clause),ValuesBase.return_defaults()
is intended only for an “ORM-style” single-row INSERT/UPDATE statement. The row returned by the statement is also consumed implicitly whenValuesBase.return_defaults()
is used. By contrast,UpdateBase.returning()
leaves the RETURNING result-set intact with a collection of any number of rows.It is compatible with the existing logic to fetch auto-generated primary key values, also known as “implicit returning”. Backends that support RETURNING will automatically make use of RETURNING in order to fetch the value of newly generated primary keys; while the
UpdateBase.returning()
method circumvents this behavior,ValuesBase.return_defaults()
leaves it intact.It can be called against any backend. Backends that don’t support RETURNING will skip the usage of the feature, rather than raising an exception. The return value of
ResultProxy.returned_defaults
will beNone
ValuesBase.return_defaults()
is used by the ORM to provide an efficient implementation for theeager_defaults
feature ofmapper()
.- Parameters:
cols – optional list of column key names or
Column
objects. If omitted, all column expressions evaluated on the server are added to the returning list.
New in version 0.9.0.
-
method
sqlalchemy.sql.expression.ValuesBase.
values(*args, **kwargs)¶ specify a fixed VALUES clause for an INSERT statement, or the SET clause for an UPDATE.
Note that the
Insert
andUpdate
constructs support per-execution time formatting of the VALUES and/or SET clauses, based on the arguments passed toConnection.execute()
. However, theValuesBase.values()
method can be used to “fix” a particular set of parameters into the statement.Multiple calls to
ValuesBase.values()
will produce a new construct, each one with the parameter list modified to include the new parameters sent. In the typical case of a single dictionary of parameters, the newly passed keys will replace the same keys in the previous construct. In the case of a list-based “multiple values” construct, each new list of values is extended onto the existing list of values.- Parameters:
**kwargs –
key value pairs representing the string key of a
Column
mapped to the value to be rendered into the VALUES or SET clause:users.insert().values(name="some name") users.update().where(users.c.id==5).values(name="some name")
*args –
As an alternative to passing key/value parameters, a dictionary, tuple, or list of dictionaries or tuples can be passed as a single positional argument in order to form the VALUES or SET clause of the statement. The forms that are accepted vary based on whether this is an
Insert
or anUpdate
construct.For either an
Insert
orUpdate
construct, a single dictionary can be passed, which works the same as that of the kwargs form:users.insert().values({"name": "some name"}) users.update().values({"name": "some new name"})
Also for either form but more typically for the
Insert
construct, a tuple that contains an entry for every column in the table is also accepted:users.insert().values((5, "some name"))
The
Insert
construct also supports being passed a list of dictionaries or full-table-tuples, which on the server will render the less common SQL syntax of “multiple values” - this syntax is supported on backends such as SQLite, PostgreSQL, MySQL, but not necessarily others:users.insert().values([ {"name": "some name"}, {"name": "some other name"}, {"name": "yet another name"}, ])
The above form would render a multiple VALUES statement similar to:
INSERT INTO users (name) VALUES (:name_1), (:name_2), (:name_3)
It is essential to note that passing multiple values is NOT the same as using traditional executemany() form. The above syntax is a special syntax not typically used. To emit an INSERT statement against multiple rows, the normal method is to pass a multiple values list to the
Connection.execute()
method, which is supported by all database backends and is generally more efficient for a very large number of parameters.See also
Executing Multiple Statements - an introduction to the traditional Core method of multiple parameter set invocation for INSERTs and other statements.
Changed in version 1.0.0: an INSERT that uses a multiple-VALUES clause, even a list of length one, implies that the
Insert.inline
flag is set to True, indicating that the statement will not attempt to fetch the “last inserted primary key” or other defaults. The statement deals with an arbitrary number of rows, so theResultProxy.inserted_primary_key
accessor does not apply.Changed in version 1.0.0: A multiple-VALUES INSERT now supports columns with Python side default values and callables in the same way as that of an “executemany” style of invocation; the callable is invoked for each row. See Python-side defaults invoked for each row individually when using a multivalued insert for other details.
The
Update
construct supports a special form which is a list of 2-tuples, which when provided must be passed in conjunction with theupdate.preserve_parameter_order
parameter. This form causes the UPDATE statement to render the SET clauses using the order of parameters given toUpdate.values()
, rather than the ordering of columns given in theTable
.New in version 1.0.10: - added support for parameter-ordered UPDATE statements via the
update.preserve_parameter_order
flag.See also
Parameter-Ordered Updates - full example of the
update.preserve_parameter_order
flag
See also
Inserts, Updates and Deletes - SQL Expression Language Tutorial
insert()
- produce anINSERT
statementupdate()
- produce anUPDATE
statement
-
method