]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Fix and add tests
authorAlonM <alon.menczer@gmail.com>
Sat, 14 Nov 2020 16:14:15 +0000 (18:14 +0200)
committerAlonM <alon.menczer@gmail.com>
Sat, 14 Nov 2020 16:14:15 +0000 (18:14 +0200)
lib/sqlalchemy/dialects/postgresql/base.py
lib/sqlalchemy/dialects/postgresql/ext.py
test/dialect/postgresql/test_compiler.py

index 772f9f553cd8be2ceb7889216b8f1b16e67deb81..0ba3d0a6385b694d88d54a167652721d11d53410 100644 (file)
@@ -2418,7 +2418,7 @@ class PGDDLCompiler(compiler.DDLCompiler):
         for expr, name, op in constraint._render_exprs:
             kw["include_table"] = False
             exclude_element = self.sql_compiler.process(expr, **kw) + (
-                (" " + constraint.ops[expr.key] + " ")
+                (" " + constraint.ops[expr.key])
                 if hasattr(expr, "key") and expr.key in constraint.ops
                 else ""
             )
index 42e1a5c70fce0f85febc3869175e775fbe02f52b..d2c6e81a326e53bb2f81e1a81485a42259ca49ba 100644 (file)
@@ -209,8 +209,7 @@ class ExcludeConstraint(ColumnCollectionConstraint):
         if where is not None:
             self.where = coercions.expect(roles.StatementOptionRole, where)
 
-        # TODO: add docs, name?
-        self.ops = kw.get("ops")
+        self.ops = kw.get("ops", {})
 
     def _set_parent(self, table, **kw):
         super(ExcludeConstraint, self)._set_parent(table)
index dad7ccd3cb39c6a1ad022b26245024106d0c9c25..a031c3df93ea417f681ddc6c62dbb83b56fbb1d5 100644 (file)
@@ -820,13 +820,14 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
             where="room > 100",
             deferrable=True,
             initially="immediate",
+            ops={"room": "my_opclass"},
         )
         tbl.append_constraint(cons)
         self.assert_compile(
             schema.AddConstraint(cons),
             "ALTER TABLE testtbl ADD CONSTRAINT my_name "
             "EXCLUDE USING gist "
-            "(room WITH =, during WITH "
+            "(room my_opclass WITH =, during WITH "
             "&&) WHERE "
             "(room > 100) DEFERRABLE INITIALLY immediate",
             dialect=postgresql.dialect(),
@@ -935,6 +936,24 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
             dialect=postgresql.dialect(),
         )
 
+    def test_exclude_constraint_ops_many(self):
+        m = MetaData()
+        tbl = Table(
+            "testtbl", m, Column("room", String), Column("during", TSRANGE)
+        )
+        cons = ExcludeConstraint(
+            ("room", "="),
+            ("during", "&&"),
+            ops={"room": "first_opsclass", "during": "second_opclass"},
+        )
+        tbl.append_constraint(cons)
+        self.assert_compile(
+            schema.AddConstraint(cons),
+            "ALTER TABLE testtbl ADD EXCLUDE USING gist "
+            "(room first_opsclass WITH =, during second_opclass WITH &&)",
+            dialect=postgresql.dialect(),
+        )
+
     def test_substring(self):
         self.assert_compile(
             func.substring("abc", 1, 2),