PostgreSQL-specific implementation of INSERT. Adds methods for PG-specific syntaxes such as ON CONFLICT. The :class:`_postgresql.Insert` object is created using the :func:`sqlalchemy.dialects.postgresql.insert` function.
| 59 | |
| 60 | |
| 61 | class Insert(StandardInsert): |
| 62 | """PostgreSQL-specific implementation of INSERT. |
| 63 | |
| 64 | Adds methods for PG-specific syntaxes such as ON CONFLICT. |
| 65 | |
| 66 | The :class:`_postgresql.Insert` object is created using the |
| 67 | :func:`sqlalchemy.dialects.postgresql.insert` function. |
| 68 | |
| 69 | """ |
| 70 | |
| 71 | stringify_dialect = "postgresql" |
| 72 | inherit_cache = False |
| 73 | |
| 74 | @util.memoized_property |
| 75 | def excluded( |
| 76 | self, |
| 77 | ) -> ReadOnlyColumnCollection[str, KeyedColumnElement[Any]]: |
| 78 | """Provide the ``excluded`` namespace for an ON CONFLICT statement |
| 79 | |
| 80 | PG's ON CONFLICT clause allows reference to the row that would |
| 81 | be inserted, known as ``excluded``. This attribute provides |
| 82 | all columns in this row to be referenceable. |
| 83 | |
| 84 | .. tip:: The :attr:`_postgresql.Insert.excluded` attribute is an |
| 85 | instance of :class:`_expression.ColumnCollection`, which provides |
| 86 | an interface the same as that of the :attr:`_schema.Table.c` |
| 87 | collection described at :ref:`metadata_tables_and_columns`. |
| 88 | With this collection, ordinary names are accessible like attributes |
| 89 | (e.g. ``stmt.excluded.some_column``), but special names and |
| 90 | dictionary method names should be accessed using indexed access, |
| 91 | such as ``stmt.excluded["column name"]`` or |
| 92 | ``stmt.excluded["values"]``. See the docstring for |
| 93 | :class:`_expression.ColumnCollection` for further examples. |
| 94 | |
| 95 | .. seealso:: |
| 96 | |
| 97 | :ref:`postgresql_insert_on_conflict` - example of how |
| 98 | to use :attr:`_expression.Insert.excluded` |
| 99 | |
| 100 | """ |
| 101 | return alias(self.table, name="excluded").columns |
| 102 | |
| 103 | _on_conflict_exclusive = _exclusive_against( |
| 104 | "_post_values_clause", |
| 105 | msgs={ |
| 106 | "_post_values_clause": "This Insert construct already has " |
| 107 | "an ON CONFLICT clause established" |
| 108 | }, |
| 109 | ) |
| 110 | |
| 111 | @_generative |
| 112 | @_on_conflict_exclusive |
| 113 | def on_conflict_do_update( |
| 114 | self, |
| 115 | constraint: _OnConflictConstraintT = None, |
| 116 | index_elements: _OnConflictIndexElementsT = None, |
| 117 | index_where: _OnConflictIndexWhereT = None, |
| 118 | set_: _OnConflictSetT = None, |
no test coverage detected