--- /dev/null
+.. change::
+ :tags: bug, postgresql
+ :tickets: 6696
+
+ Fixed issue in :meth:`_postgresql.Insert.on_conflict_do_nothing` and
+ :meth:`_postgresql.Insert.on_conflict_do_update` where the name of a unique
+ constraint passed as the ``constraint`` parameter would not be properly
+ quoted if it contained characters which required quoting.
+
def _on_conflict_target(self, clause, **kw):
if clause.constraint_target is not None:
- target_text = "ON CONSTRAINT %s" % clause.constraint_target
+ target_text = "ON CONSTRAINT %s" % self.preparer.quote(
+ clause.constraint_target
+ )
elif clause.inferred_target_elements is not None:
target_text = "(%s)" % ", ".join(
(
from sqlalchemy import text
from sqlalchemy import tuple_
from sqlalchemy import types as sqltypes
+from sqlalchemy import UniqueConstraint
from sqlalchemy import update
from sqlalchemy.dialects import postgresql
from sqlalchemy.dialects.postgresql import aggregate_order_by
"DO UPDATE SET myid = excluded.myid",
)
+ def test_do_nothing_quoted_string_constraint_target(self):
+ """test #6696"""
+ i = insert(self.table1, values=dict(name="foo"))
+ i = i.on_conflict_do_nothing(constraint="Some Constraint Name")
+ self.assert_compile(
+ i,
+ "INSERT INTO mytable (name) VALUES "
+ '(%(name)s) ON CONFLICT ON CONSTRAINT "Some Constraint Name" '
+ "DO NOTHING",
+ )
+
+ def test_do_nothing_quoted_named_constraint_target(self):
+ """test #6696"""
+ i = insert(self.table1, values=dict(name="foo"))
+ unique_constr = UniqueConstraint(
+ self.table1.c.myid, name="Some Constraint Name"
+ )
+ i = i.on_conflict_do_nothing(constraint=unique_constr)
+ self.assert_compile(
+ i,
+ "INSERT INTO mytable (name) VALUES "
+ '(%(name)s) ON CONFLICT ON CONSTRAINT "Some Constraint Name" '
+ "DO NOTHING",
+ )
+
def test_do_update_index_elements_where_target(self):
i = insert(self.table1, values=dict(name="foo"))
i = i.on_conflict_do_update(