]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
ExcludeConstraint literal_compile
authorFederico Caselli <cfederico87@gmail.com>
Wed, 22 Feb 2023 20:57:19 +0000 (21:57 +0100)
committerFederico Caselli <cfederico87@gmail.com>
Wed, 22 Feb 2023 20:57:19 +0000 (21:57 +0100)
ExcludeConstraint correctly uses literal compile
when compiling expression ddl.

Fixes: #9349
Change-Id: I11a994ac46556a972afc696a2baad7ddbdd3de97

doc/build/changelog/unreleased_20/9349.rst [new file with mode: 0644]
lib/sqlalchemy/dialects/postgresql/base.py
lib/sqlalchemy/dialects/postgresql/ext.py
test/dialect/postgresql/test_compiler.py

diff --git a/doc/build/changelog/unreleased_20/9349.rst b/doc/build/changelog/unreleased_20/9349.rst
new file mode 100644 (file)
index 0000000..957ac56
--- /dev/null
@@ -0,0 +1,6 @@
+.. change::
+    :tags: bug, schema, postgresql
+    :tickets: 9332
+
+    ExcludeConstraint correctly uses literal compile when compiling
+    expression ddl.
index 255c72042e7b6b157a547210083e3f450c847328..3ba10380264a20cda62e6967387945c5f1734573 100644 (file)
@@ -2350,8 +2350,9 @@ class PGDDLCompiler(compiler.DDLCompiler):
                 constraint
             )
         elements = []
+        kw["include_table"] = False
+        kw["literal_binds"] = True
         for expr, name, op in constraint._render_exprs:
-            kw["include_table"] = False
             exclude_element = self.sql_compiler.process(expr, **kw) + (
                 (" " + constraint.ops[expr.key])
                 if hasattr(expr, "key") and expr.key in constraint.ops
index 0f5efb1de980dbcc81abf0620960e4f876f2ce50..22604955dcce065d91eba702034f2fc3d2932d50 100644 (file)
@@ -164,16 +164,15 @@ class ExcludeConstraint(ColumnCollectionConstraint):
         :param \*elements:
 
           A sequence of two tuples of the form ``(column, operator)`` where
-          "column" is a SQL expression element or a raw SQL string, most
-          typically a :class:`_schema.Column` object,
-          and "operator" is a string
-          containing the operator to use.   In order to specify a column name
-          when a  :class:`_schema.Column` object is not available,
-          while ensuring
+          "column" is a SQL expression element or the name of a column as
+          string, most typically a :class:`_schema.Column` object,
+          and "operator" is a string containing the operator to use.
+          In order to specify a column name when a :class:`_schema.Column`
+          object is not available, while ensuring
           that any necessary quoting rules take effect, an ad-hoc
           :class:`_schema.Column` or :func:`_expression.column`
-          object should be
-          used.
+          object should be used. ``column`` may also be a string SQL
+          expression when passed as :func:`_expression.literal_column`
 
         :param name:
           Optional, the in-database name of this constraint.
index 080cfb767d54c3e19e616c803bd3d5a3abbc6889..e8bead008fcce8807f11e3d795a1d58ea528a5a1 100644 (file)
@@ -1034,7 +1034,7 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
         tbl.append_constraint(cons)
         self.assert_compile(
             schema.AddConstraint(cons),
-            "ALTER TABLE testtbl ADD EXCLUDE USING gist " "(room WITH =)",
+            "ALTER TABLE testtbl ADD EXCLUDE USING gist (room WITH =)",
             dialect=postgresql.dialect(),
         )
 
@@ -1134,7 +1134,7 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
         tbl.append_constraint(cons_copy)
         self.assert_compile(
             schema.AddConstraint(cons_copy),
-            "ALTER TABLE testtbl ADD EXCLUDE USING gist " "(room WITH =)",
+            "ALTER TABLE testtbl ADD EXCLUDE USING gist (room WITH =)",
         )
 
     def test_exclude_constraint_copy_where_using(self):
@@ -1243,6 +1243,21 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
             dialect=postgresql.dialect(),
         )
 
+    def test_exclude_constraint_literal_binds(self):
+        m = MetaData()
+        tbl = Table("foo", m, Column("x", Integer), Column("y", Integer))
+        cons = ExcludeConstraint(
+            (func.power(tbl.c.x, 42), "="),
+            (func.int8range(column("x"), "y"), "&&"),
+        )
+        tbl.append_constraint(cons)
+        self.assert_compile(
+            schema.AddConstraint(cons),
+            "ALTER TABLE foo ADD EXCLUDE USING gist "
+            "(power(x, 42) WITH =, int8range(x, 'y') WITH &&)",
+            dialect=postgresql.dialect(),
+        )
+
     def test_substring(self):
         self.assert_compile(
             func.substring("abc", 1, 2),