MySQL-specific implementation of INSERT. Adds methods for MySQL-specific syntaxes such as ON DUPLICATE KEY UPDATE. The :class:`~.mysql.Insert` object is created using the :func:`sqlalchemy.dialects.mysql.insert` function.
| 103 | |
| 104 | |
| 105 | class Insert(StandardInsert): |
| 106 | """MySQL-specific implementation of INSERT. |
| 107 | |
| 108 | Adds methods for MySQL-specific syntaxes such as ON DUPLICATE KEY UPDATE. |
| 109 | |
| 110 | The :class:`~.mysql.Insert` object is created using the |
| 111 | :func:`sqlalchemy.dialects.mysql.insert` function. |
| 112 | |
| 113 | """ |
| 114 | |
| 115 | stringify_dialect = "mysql" |
| 116 | inherit_cache = True |
| 117 | |
| 118 | @property |
| 119 | def inserted( |
| 120 | self, |
| 121 | ) -> ReadOnlyColumnCollection[str, KeyedColumnElement[Any]]: |
| 122 | """Provide the "inserted" namespace for an ON DUPLICATE KEY UPDATE |
| 123 | statement |
| 124 | |
| 125 | MySQL's ON DUPLICATE KEY UPDATE clause allows reference to the row |
| 126 | that would be inserted, via a special function called ``VALUES()``. |
| 127 | This attribute provides all columns in this row to be referenceable |
| 128 | such that they will render within a ``VALUES()`` function inside the |
| 129 | ON DUPLICATE KEY UPDATE clause. The attribute is named ``.inserted`` |
| 130 | so as not to conflict with the existing |
| 131 | :meth:`_expression.Insert.values` method. |
| 132 | |
| 133 | .. tip:: The :attr:`_mysql.Insert.inserted` attribute is an instance |
| 134 | of :class:`_expression.ColumnCollection`, which provides an |
| 135 | interface the same as that of the :attr:`_schema.Table.c` |
| 136 | collection described at :ref:`metadata_tables_and_columns`. |
| 137 | With this collection, ordinary names are accessible like attributes |
| 138 | (e.g. ``stmt.inserted.some_column``), but special names and |
| 139 | dictionary method names should be accessed using indexed access, |
| 140 | such as ``stmt.inserted["column name"]`` or |
| 141 | ``stmt.inserted["values"]``. See the docstring for |
| 142 | :class:`_expression.ColumnCollection` for further examples. |
| 143 | |
| 144 | .. seealso:: |
| 145 | |
| 146 | :ref:`mysql_insert_on_duplicate_key_update` - example of how |
| 147 | to use :attr:`_expression.Insert.inserted` |
| 148 | |
| 149 | """ |
| 150 | return self.inserted_alias.columns |
| 151 | |
| 152 | @util.memoized_property |
| 153 | def inserted_alias(self) -> NamedFromClause: |
| 154 | return alias(self.table, name="inserted") |
| 155 | |
| 156 | @_exclusive_against( |
| 157 | "_post_values_clause", |
| 158 | msgs={ |
| 159 | "_post_values_clause": "This Insert construct already " |
| 160 | "has an ON DUPLICATE KEY clause present" |
| 161 | }, |
| 162 | ) |