]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Apply SQL compilation to sqltext for column-level CHECK constraint
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 4 Apr 2017 14:02:39 +0000 (10:02 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 4 Apr 2017 14:02:39 +0000 (10:02 -0400)
Fixed bug where a column-level :class:`.CheckConstraint` would fail
to compile the SQL expression using the underlying dialect compiler
as well as apply proper flags to generate literal values as
inline, in the case that the sqltext is a Core expression and
not just a plain string.   This was long-ago fixed for table-level
check constraints in 0.9 as part of :ticket:`2742`, which more commonly
feature Core SQL expressions as opposed to plain string expressions.

Change-Id: I1301ba4b40063e91bc47726aecc5f4990ffcaeda
Fixes: #3957
doc/build/changelog/changelog_12.rst
lib/sqlalchemy/sql/compiler.py
test/sql/test_constraints.py

index 1d66adc8ef1dd7af4a0c8e18e3b0cbae0e43c4b6..ad9e73160f283c2be760c8af7ca0aad900c35ab2 100644 (file)
 .. changelog::
     :version: 1.2.0b1
 
+    .. change:: 3957
+        :tags: bug, sql
+        :tickets: 3957
+
+        Fixed bug where a column-level :class:`.CheckConstraint` would fail
+        to compile the SQL expression using the underlying dialect compiler
+        as well as apply proper flags to generate literal values as
+        inline, in the case that the sqltext is a Core expression and
+        not just a plain string.   This was long-ago fixed for table-level
+        check constraints in 0.9 as part of :ticket:`2742`, which more commonly
+        feature Core SQL expressions as opposed to plain string expressions.
+
     .. change:: 3923
         :tags: bug, sql
         :tickets: 3923
index 4415028988d081def37366b79d3142a167674642..b18f90312f59c182336d648ea51c174c99bed812 100644 (file)
@@ -2602,7 +2602,9 @@ class DDLCompiler(Compiled):
             formatted_name = self.preparer.format_constraint(constraint)
             if formatted_name is not None:
                 text += "CONSTRAINT %s " % formatted_name
-        text += "CHECK (%s)" % constraint.sqltext
+        text += "CHECK (%s)" % self.sql_compiler.process(constraint.sqltext,
+                                                         include_table=False,
+                                                         literal_binds=True)
         text += self.define_constraint_deferrability(constraint)
         return text
 
index aebbb4c3055e44f709b8fafa8a04b8954058753c..3365b3cf0d5bafbee2fcf429d310b038f8bb2df0 100644 (file)
@@ -1137,6 +1137,19 @@ class ConstraintCompilationTest(fixtures.TestBase, AssertsCompiledSQL):
             "ALTER TABLE tbl ADD CHECK (a > 5)"
         )
 
+    def test_render_check_constraint_inline_sql_literal(self):
+        t, t2 = self._constraint_create_fixture()
+
+        m = MetaData()
+        t = Table(
+            't', m,
+            Column('a', Integer, CheckConstraint(Column('a', Integer) > 5)))
+
+        self.assert_compile(
+            schema.CreateColumn(t.c.a),
+            "a INTEGER CHECK (a > 5)"
+        )
+
     def test_render_index_sql_literal(self):
         t, t2 = self._constraint_create_fixture()