# SQLAlchemy 0.9 Documentation

Search:
Release: 0.9.3 | Release Date: February 19, 2014

# 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`.

`sqlalchemy.sql.expression.``delete`(table, whereclause=None, bind=None, returning=None, prefixes=None, **dialect_kw)

  Construct `Delete` object.

  Similar functionality is available via the `delete()` method on `Table`.

  Parameters: | table – The table to be updated. whereclause – A `ClauseElement` describing the `WHERE` condition of the `UPDATE` statement. Note that the `where()` generative method may be used instead.

  See also

  Deletes - SQL Expression Tutorial

`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 `insert()` method on `Table`.

  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; a `Insert` construct will also dynamically render the VALUES clause at execution time based on the parameters passed to `Connection.execute()`. inline – if True, SQL defaults will be compiled ‘inline’ into the statement and not pre-executed.

  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 this `INSERT` statement’s table, the statement will be correlated against the `INSERT` statement.

  See also

  Insert Expressions - SQL Expression Tutorial

  Inserts, Updates and Deletes - SQL Expression Tutorial

`sqlalchemy.sql.expression.``update`(table, whereclause=None, values=None, inline=False, bind=None, prefixes=None, returning=None, return_defaults=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 `update()` method on `Table`:

      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 the `UPDATE` statement. Modern applications may prefer to use the generative `where()` method to specify the `WHERE` 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()
                )
  Changed in version 0.7.4: The WHERE clause can refer to multiple tables. values – Optional dictionary which specifies the `SET` conditions of the `UPDATE`. If left as `None`, the `SET` 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, the `SET` 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 the `default` 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 from `ResultProxy.last_updated_params()`.

  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 (specifically the “key” of the `Column`, normally but not necessarily equivalent to its “name”). Normally, the `Column` objects used here are expected to be part of the target `Table` 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-returning `select()` construct, etc.

  When combining `select()` constructs within the values clause of an `update()` construct, the subquery represented by the `select()` 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)

  Bases: `sqlalchemy.sql.expression.UpdateBase`

  Represent a DELETE construct.

  The `Delete` object is created using the `delete()` function.

  `__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.

  `bind`
    inherited from the `bind` attribute of `UpdateBase`

    Return a ‘bind’ linked to this `UpdateBase` or a `Table` associated with it.

  `compare`(other, **kw)
    inherited from the `compare()` method of `ClauseElement`

    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`)

  `compile`(bind=None, dialect=None, **kw)
    inherited from the `compile()` method of `ClauseElement`

    Compile this SQL expression.

    The return value is a `Compiled` object. Calling `str()` or `unicode()` on the returned value will yield a string representation of the result. The `Compiled` object also can return a dictionary of bind parameter names and values using the `params` accessor.

    Parameters: | bind – An `Engine` or `Connection` from which a `Compiled` will be acquired. This argument takes precedence over this `ClauseElement`‘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 a `Compiled` will be acquired. This argument takes precedence over the bind argument as well as this `ClauseElement`‘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.

  `dialect_kwargs`
    inherited from the `dialect_kwargs` attribute of `DialectKWArgs`

    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 the `DialectKWArgs.dialect_options` collection, which contains all options known by this dialect including defaults.

    New in version 0.9.2.

    See also

    `DialectKWArgs.dialect_options` - nested dictionary form

  `dialect_options`
    inherited from the `dialect_options` attribute of `DialectKWArgs`

    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, the `postgresql_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

  `execute`(*multiparams, **params)
    inherited from the `execute()` method of `Executable`

    Compile and execute this `Executable`.

  `execution_options`(**kw)
    inherited from the `execution_options()` method of `Executable`

    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, the `Engine` and ORM `Query` 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.

    See also

    `Connection.execution_options()`

    `Query.execution_options()`

  `kwargs`
    inherited from the `kwargs` attribute of `DialectKWArgs`

    Deprecated; see :attr:`.DialectKWArgs.dialect_kwargs

  `params`(*arg, **kw)
    inherited from the `params()` method of `UpdateBase`

    Set the parameters for the statement.

    This method raises `NotImplementedError` on the base class, and is overridden by `ValuesBase` to provide the SET/VALUES clause of UPDATE and INSERT.

  `prefix_with`(*expr, **kw)
    inherited from the `prefix_with()` method of `HasPrefixes`

    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.

  `returning`(*cols)
    inherited from the `returning()` method of `UpdateBase`

    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.

  `scalar`(*multiparams, **params)
    inherited from the `scalar()` method of `Executable`

    Compile and execute this `Executable`, returning the result’s scalar representation.

  `self_group`(against=None)
    inherited from the `self_group()` method of `ClauseElement`

    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 another `select()`. (Note that subqueries should be normally created using the `Select.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 like `x OR (y AND z)` - AND takes precedence over OR.

    The base `self_group()` method of `ClauseElement` just returns self.

  `unique_params`(*optionaldict, **kwargs)
    inherited from the `unique_params()` method of `ClauseElement`

    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.

  `where`(whereclause)

    Add the given WHERE clause to a newly returned delete construct.

  `with_hint`(text, selectable=None, dialect_name='*')
    inherited from the `with_hint()` method of `UpdateBase`

    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, use `UpdateBase.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 given `Table` passed as the `selectable` 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")

    New in version 0.7.6.

    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.

class `sqlalchemy.sql.expression.``Insert`(table, values=None, inline=False, bind=None, prefixes=None, returning=None, return_defaults=False, **dialect_kw)

  Bases: `sqlalchemy.sql.expression.ValuesBase`

  Represent an INSERT construct.

  The `Insert` object is created using the `insert()` function.

  See also

  Insert Expressions

  `__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.

  `bind`
    inherited from the `bind` attribute of `UpdateBase`

    Return a ‘bind’ linked to this `UpdateBase` or a `Table` associated with it.

  `compare`(other, **kw)
    inherited from the `compare()` method of `ClauseElement`

    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`)

  `compile`(bind=None, dialect=None, **kw)
    inherited from the `compile()` method of `ClauseElement`

    Compile this SQL expression.

    The return value is a `Compiled` object. Calling `str()` or `unicode()` on the returned value will yield a string representation of the result. The `Compiled` object also can return a dictionary of bind parameter names and values using the `params` accessor.

    Parameters: | bind – An `Engine` or `Connection` from which a `Compiled` will be acquired. This argument takes precedence over this `ClauseElement`‘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 a `Compiled` will be acquired. This argument takes precedence over the bind argument as well as this `ClauseElement`‘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.

  `dialect_kwargs`
    inherited from the `dialect_kwargs` attribute of `DialectKWArgs`

    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 the `DialectKWArgs.dialect_options` collection, which contains all options known by this dialect including defaults.

    New in version 0.9.2.

    See also

    `DialectKWArgs.dialect_options` - nested dictionary form

  `dialect_options`
    inherited from the `dialect_options` attribute of `DialectKWArgs`

    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, the `postgresql_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

  `execute`(*multiparams, **params)
    inherited from the `execute()` method of `Executable`

    Compile and execute this `Executable`.

  `execution_options`(**kw)
    inherited from the `execution_options()` method of `Executable`

    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, the `Engine` and ORM `Query` 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.

    See also

    `Connection.execution_options()`

    `Query.execution_options()`

  `from_select`(names, select)

    Return a new `Insert` construct which represents an `INSERT...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 a `FromClause`, such as an ORM `Query` object, etc. The order of columns returned from this FROM clause should correspond to the order of columns sent as the `names` 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.

    Note

    Depending on backend, it may be necessary for the `Insert` statement to be constructed using the `inline=True` flag; this flag will prevent the implicit usage of `RETURNING` when the `INSERT` statement is rendered, which isn’t supported on a backend such as Oracle in conjunction with an `INSERT..SELECT` combination:

        sel = select([table1.c.a, table1.c.b]).where(table1.c.c > 5)
        ins = table2.insert(inline=True).from_select(['a', 'b'], sel)

    Note

    A SELECT..INSERT construct in SQL has no VALUES clause. Therefore `Column` objects which utilize Python-side defaults (e.g. as described at metadata_defaults_toplevel) will not take effect when using `Insert.from_select()`.

    New in version 0.8.3.

  `kwargs`
    inherited from the `kwargs` attribute of `DialectKWArgs`

    Deprecated; see :attr:`.DialectKWArgs.dialect_kwargs

  `params`(*arg, **kw)
    inherited from the `params()` method of `UpdateBase`

    Set the parameters for the statement.

    This method raises `NotImplementedError` on the base class, and is overridden by `ValuesBase` to provide the SET/VALUES clause of UPDATE and INSERT.

  `prefix_with`(*expr, **kw)
    inherited from the `prefix_with()` method of `HasPrefixes`

    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.

  `return_defaults`(*cols)
    inherited from the `return_defaults()` method of `ValuesBase`

    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 the `ResultProxy.returned_defaults` accessor as a dictionary, referring to values keyed to the `Column` object as well as its `.key`.

    This method differs from `UpdateBase.returning()` in these ways:

    1. `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 implcitly when `ValuesBase.return_defaults()` is used. By contrast, `UpdateBase.returning()` leaves the RETURNING result-set intact with a collection of any number of rows.
    2. 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.
    3. 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 be `None`

    `ValuesBase.return_defaults()` is used by the ORM to provide an efficient implementation for the `eager_defaults` feature of `mapper()`.

    Parameters: | cols – optional list of column key names or `Column` objects. If omitted, all column expressions evaulated on the server are added to the returning list.

    New in version 0.9.0.

    See also

    `UpdateBase.returning()`

    `ResultProxy.returned_defaults`

  `returning`(*cols)
    inherited from the `returning()` method of `UpdateBase`

    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.

  `scalar`(*multiparams, **params)
    inherited from the `scalar()` method of `Executable`

    Compile and execute this `Executable`, returning the result’s scalar representation.

  `self_group`(against=None)
    inherited from the `self_group()` method of `ClauseElement`

    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 another `select()`. (Note that subqueries should be normally created using the `Select.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 like `x OR (y AND z)` - AND takes precedence over OR.

    The base `self_group()` method of `ClauseElement` just returns self.

  `unique_params`(*optionaldict, **kwargs)
    inherited from the `unique_params()` method of `ClauseElement`

    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.

  `values`(*args, **kwargs)
    inherited from the `values()` method of `ValuesBase`

    specify a fixed VALUES clause for an INSERT statement, or the SET clause for an UPDATE.

    Note that the `Insert` and `Update` constructs support per-execution time formatting of the VALUES and/or SET clauses, based on the arguments passed to `Connection.execute()`. However, the `ValuesBase.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 – Alternatively, 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 single dictionary form works the same as the kwargs form:
          users.insert().values({"name": "some name"})
      If a tuple is passed, the tuple should contain the same number of columns as the target `Table`:
          users.insert().values((5, "some name"))
      The `Insert` construct also supports multiply-rendered VALUES construct, for those backends which support this SQL syntax (SQLite, Postgresql, MySQL). This mode is indicated by passing a list of one or more dictionaries/tuples:
          users.insert().values([
                              {"name": "some name"},
                              {"name": "some other name"},
                              {"name": "yet another name"},
                          ])
      In the case of an `Update` construct, only the single dictionary/tuple form is accepted, else an exception is raised. It is also an exception case to attempt to mix the single-/multiple- value styles together, either through multiple `ValuesBase.values()` calls or by sending a list + kwargs at the same time. Note Passing a multiple values list is not the same as passing a multiple values list to the `Connection.execute()` method. Passing a list of parameter sets to `ValuesBase.values()` produces a construct of this form:
          INSERT INTO table (col1, col2, col3) VALUES
                          (col1_0, col2_0, col3_0),
                          (col1_1, col2_1, col3_1),
                          ...
    whereas a multiple list passed to `Connection.execute()` has the effect of using the DBAPI executemany() method, which provides a high-performance system of invoking a single-row INSERT statement many times against a series of parameter sets. The “executemany” style is supported by all database backends, as it does not depend on a special SQL syntax. New in version 0.8: Support for multiple-VALUES INSERT statements.

    See also

    Inserts, Updates and Deletes - SQL Expression Language Tutorial

    `insert()` - produce an `INSERT` statement

    `update()` - produce an `UPDATE` statement

  `with_hint`(text, selectable=None, dialect_name='*')
    inherited from the `with_hint()` method of `UpdateBase`

    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, use `UpdateBase.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 given `Table` passed as the `selectable` 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")

    New in version 0.7.6.

    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.

class `sqlalchemy.sql.expression.``Update`(table, whereclause=None, values=None, inline=False, bind=None, prefixes=None, returning=None, return_defaults=False, **dialect_kw)

  Bases: `sqlalchemy.sql.expression.ValuesBase`

  Represent an Update construct.

  The `Update` object is created using the `update()` function.

  `__init__`(table, whereclause=None, values=None, inline=False, bind=None, prefixes=None, returning=None, return_defaults=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.

  `bind`
    inherited from the `bind` attribute of `UpdateBase`

    Return a ‘bind’ linked to this `UpdateBase` or a `Table` associated with it.

  `compare`(other, **kw)
    inherited from the `compare()` method of `ClauseElement`

    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`)

  `compile`(bind=None, dialect=None, **kw)
    inherited from the `compile()` method of `ClauseElement`

    Compile this SQL expression.

    The return value is a `Compiled` object. Calling `str()` or `unicode()` on the returned value will yield a string representation of the result. The `Compiled` object also can return a dictionary of bind parameter names and values using the `params` accessor.

    Parameters: | bind – An `Engine` or `Connection` from which a `Compiled` will be acquired. This argument takes precedence over this `ClauseElement`‘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 a `Compiled` will be acquired. This argument takes precedence over the bind argument as well as this `ClauseElement`‘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.

  `dialect_kwargs`
    inherited from the `dialect_kwargs` attribute of `DialectKWArgs`

    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 the `DialectKWArgs.dialect_options` collection, which contains all options known by this dialect including defaults.

    New in version 0.9.2.

    See also

    `DialectKWArgs.dialect_options` - nested dictionary form

  `dialect_options`
    inherited from the `dialect_options` attribute of `DialectKWArgs`

    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, the `postgresql_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

  `execute`(*multiparams, **params)
    inherited from the `execute()` method of `Executable`

    Compile and execute this `Executable`.

  `execution_options`(**kw)
    inherited from the `execution_options()` method of `Executable`

    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, the `Engine` and ORM `Query` 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.

    See also

    `Connection.execution_options()`

    `Query.execution_options()`

  `kwargs`
    inherited from the `kwargs` attribute of `DialectKWArgs`

    Deprecated; see :attr:`.DialectKWArgs.dialect_kwargs

  `params`(*arg, **kw)
    inherited from the `params()` method of `UpdateBase`

    Set the parameters for the statement.

    This method raises `NotImplementedError` on the base class, and is overridden by `ValuesBase` to provide the SET/VALUES clause of UPDATE and INSERT.

  `prefix_with`(*expr, **kw)
    inherited from the `prefix_with()` method of `HasPrefixes`

    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.

  `return_defaults`(*cols)
    inherited from the `return_defaults()` method of `ValuesBase`

    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 the `ResultProxy.returned_defaults` accessor as a dictionary, referring to values keyed to the `Column` object as well as its `.key`.

    This method differs from `UpdateBase.returning()` in these ways:

    1. `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 implcitly when `ValuesBase.return_defaults()` is used. By contrast, `UpdateBase.returning()` leaves the RETURNING result-set intact with a collection of any number of rows.
    2. 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.
    3. 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 be `None`

    `ValuesBase.return_defaults()` is used by the ORM to provide an efficient implementation for the `eager_defaults` feature of `mapper()`.

    Parameters: | cols – optional list of column key names or `Column` objects. If omitted, all column expressions evaulated on the server are added to the returning list.

    New in version 0.9.0.

    See also

    `UpdateBase.returning()`

    `ResultProxy.returned_defaults`

  `returning`(*cols)
    inherited from the `returning()` method of `UpdateBase`

    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.

  `scalar`(*multiparams, **params)
    inherited from the `scalar()` method of `Executable`

    Compile and execute this `Executable`, returning the result’s scalar representation.

  `self_group`(against=None)
    inherited from the `self_group()` method of `ClauseElement`

    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 another `select()`. (Note that subqueries should be normally created using the `Select.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 like `x OR (y AND z)` - AND takes precedence over OR.

    The base `self_group()` method of `ClauseElement` just returns self.

  `unique_params`(*optionaldict, **kwargs)
    inherited from the `unique_params()` method of `ClauseElement`

    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.

  `values`(*args, **kwargs)
    inherited from the `values()` method of `ValuesBase`

    specify a fixed VALUES clause for an INSERT statement, or the SET clause for an UPDATE.

    Note that the `Insert` and `Update` constructs support per-execution time formatting of the VALUES and/or SET clauses, based on the arguments passed to `Connection.execute()`. However, the `ValuesBase.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 – Alternatively, 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 single dictionary form works the same as the kwargs form:
          users.insert().values({"name": "some name"})
      If a tuple is passed, the tuple should contain the same number of columns as the target `Table`:
          users.insert().values((5, "some name"))
      The `Insert` construct also supports multiply-rendered VALUES construct, for those backends which support this SQL syntax (SQLite, Postgresql, MySQL). This mode is indicated by passing a list of one or more dictionaries/tuples:
          users.insert().values([
                              {"name": "some name"},
                              {"name": "some other name"},
                              {"name": "yet another name"},
                          ])
      In the case of an `Update` construct, only the single dictionary/tuple form is accepted, else an exception is raised. It is also an exception case to attempt to mix the single-/multiple- value styles together, either through multiple `ValuesBase.values()` calls or by sending a list + kwargs at the same time. Note Passing a multiple values list is not the same as passing a multiple values list to the `Connection.execute()` method. Passing a list of parameter sets to `ValuesBase.values()` produces a construct of this form:
          INSERT INTO table (col1, col2, col3) VALUES
                          (col1_0, col2_0, col3_0),
                          (col1_1, col2_1, col3_1),
                          ...
    whereas a multiple list passed to `Connection.execute()` has the effect of using the DBAPI executemany() method, which provides a high-performance system of invoking a single-row INSERT statement many times against a series of parameter sets. The “executemany” style is supported by all database backends, as it does not depend on a special SQL syntax. New in version 0.8: Support for multiple-VALUES INSERT statements.

    See also

    Inserts, Updates and Deletes - SQL Expression Language Tutorial

    `insert()` - produce an `INSERT` statement

    `update()` - produce an `UPDATE` statement

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

  `with_hint`(text, selectable=None, dialect_name='*')
    inherited from the `with_hint()` method of `UpdateBase`

    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, use `UpdateBase.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 given `Table` passed as the `selectable` 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")

    New in version 0.7.6.

    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.

class `sqlalchemy.sql.expression.``UpdateBase`

  Bases: `sqlalchemy.sql.expression.DialectKWArgs`, `sqlalchemy.sql.expression.HasPrefixes`, `sqlalchemy.sql.expression.Executable`, `sqlalchemy.sql.expression.ClauseElement`

  Form the base for `INSERT`, `UPDATE`, and `DELETE` statements.

  `__init__`
    inherited from the `__init__` attribute of `object`

    x.__init__(...) initializes x; see help(type(x)) for signature

  `bind`

    Return a ‘bind’ linked to this `UpdateBase` or a `Table` associated with it.

  `compare`(other, **kw)
    inherited from the `compare()` method of `ClauseElement`

    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`)

  `compile`(bind=None, dialect=None, **kw)
    inherited from the `compile()` method of `ClauseElement`

    Compile this SQL expression.

    The return value is a `Compiled` object. Calling `str()` or `unicode()` on the returned value will yield a string representation of the result. The `Compiled` object also can return a dictionary of bind parameter names and values using the `params` accessor.

    Parameters: | bind – An `Engine` or `Connection` from which a `Compiled` will be acquired. This argument takes precedence over this `ClauseElement`‘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 a `Compiled` will be acquired. This argument takes precedence over the bind argument as well as this `ClauseElement`‘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.

  `dialect_kwargs`
    inherited from the `dialect_kwargs` attribute of `DialectKWArgs`

    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 the `DialectKWArgs.dialect_options` collection, which contains all options known by this dialect including defaults.

    New in version 0.9.2.

    See also

    `DialectKWArgs.dialect_options` - nested dictionary form

  `dialect_options`
    inherited from the `dialect_options` attribute of `DialectKWArgs`

    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, the `postgresql_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

  `execute`(*multiparams, **params)
    inherited from the `execute()` method of `Executable`

    Compile and execute this `Executable`.

  `execution_options`(**kw)
    inherited from the `execution_options()` method of `Executable`

    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, the `Engine` and ORM `Query` 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.

    See also

    `Connection.execution_options()`

    `Query.execution_options()`

  `get_children`(**kwargs)
    inherited from the `get_children()` method of `ClauseElement`

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

  `kwargs`
    inherited from the `kwargs` attribute of `DialectKWArgs`

    Deprecated; see :attr:`.DialectKWArgs.dialect_kwargs

  `params`(*arg, **kw)

    Set the parameters for the statement.

    This method raises `NotImplementedError` on the base class, and is overridden by `ValuesBase` to provide the SET/VALUES clause of UPDATE and INSERT.

  `prefix_with`(*expr, **kw)
    inherited from the `prefix_with()` method of `HasPrefixes`

    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.

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

  `scalar`(*multiparams, **params)
    inherited from the `scalar()` method of `Executable`

    Compile and execute this `Executable`, returning the result’s scalar representation.

  `self_group`(against=None)
    inherited from the `self_group()` method of `ClauseElement`

    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 another `select()`. (Note that subqueries should be normally created using the `Select.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 like `x OR (y AND z)` - AND takes precedence over OR.

    The base `self_group()` method of `ClauseElement` just returns self.

  `unique_params`(*optionaldict, **kwargs)
    inherited from the `unique_params()` method of `ClauseElement`

    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.

  `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, use `UpdateBase.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 given `Table` passed as the `selectable` 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")

    New in version 0.7.6.

    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.

class `sqlalchemy.sql.expression.``ValuesBase`(table, values, prefixes)

  Bases: `sqlalchemy.sql.expression.UpdateBase`

  Supplies support for `ValuesBase.values()` to INSERT and UPDATE constructs.

  `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 the `ResultProxy.returned_defaults` accessor as a dictionary, referring to values keyed to the `Column` object as well as its `.key`.

    This method differs from `UpdateBase.returning()` in these ways:

    1. `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 implcitly when `ValuesBase.return_defaults()` is used. By contrast, `UpdateBase.returning()` leaves the RETURNING result-set intact with a collection of any number of rows.
    2. 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.
    3. 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 be `None`

    `ValuesBase.return_defaults()` is used by the ORM to provide an efficient implementation for the `eager_defaults` feature of `mapper()`.

    Parameters: | cols – optional list of column key names or `Column` objects. If omitted, all column expressions evaulated on the server are added to the returning list.

    New in version 0.9.0.

    See also

    `UpdateBase.returning()`

    `ResultProxy.returned_defaults`

  `values`(*args, **kwargs)

    specify a fixed VALUES clause for an INSERT statement, or the SET clause for an UPDATE.

    Note that the `Insert` and `Update` constructs support per-execution time formatting of the VALUES and/or SET clauses, based on the arguments passed to `Connection.execute()`. However, the `ValuesBase.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 – Alternatively, 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 single dictionary form works the same as the kwargs form:
          users.insert().values({"name": "some name"})
      If a tuple is passed, the tuple should contain the same number of columns as the target `Table`:
          users.insert().values((5, "some name"))
      The `Insert` construct also supports multiply-rendered VALUES construct, for those backends which support this SQL syntax (SQLite, Postgresql, MySQL). This mode is indicated by passing a list of one or more dictionaries/tuples:
          users.insert().values([
                              {"name": "some name"},
                              {"name": "some other name"},
                              {"name": "yet another name"},
                          ])
      In the case of an `Update` construct, only the single dictionary/tuple form is accepted, else an exception is raised. It is also an exception case to attempt to mix the single-/multiple- value styles together, either through multiple `ValuesBase.values()` calls or by sending a list + kwargs at the same time. Note Passing a multiple values list is not the same as passing a multiple values list to the `Connection.execute()` method. Passing a list of parameter sets to `ValuesBase.values()` produces a construct of this form:
          INSERT INTO table (col1, col2, col3) VALUES
                          (col1_0, col2_0, col3_0),
                          (col1_1, col2_1, col3_1),
                          ...
    whereas a multiple list passed to `Connection.execute()` has the effect of using the DBAPI executemany() method, which provides a high-performance system of invoking a single-row INSERT statement many times against a series of parameter sets. The “executemany” style is supported by all database backends, as it does not depend on a special SQL syntax. New in version 0.8: Support for multiple-VALUES INSERT statements.

    See also

    Inserts, Updates and Deletes - SQL Expression Language Tutorial

    `insert()` - produce an `INSERT` statement

    `update()` - produce an `UPDATE` statement