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