]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- [bug] the "name" of a column-level CHECK constraint,
authorMike Bayer <mike_mp@zzzcomputing.com>
Sun, 4 Dec 2011 18:46:45 +0000 (13:46 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 4 Dec 2011 18:46:45 +0000 (13:46 -0500)
if present, is now rendered in the CREATE TABLE
statement using "CONSTRAINT <name> CHECK <expression>".
[ticket:2305]

CHANGES
lib/sqlalchemy/sql/compiler.py
test/sql/test_constraints.py

diff --git a/CHANGES b/CHANGES
index 12d4826ef7fa45ca737a94cb1e221b0e4d0450a5..b9bdfb15214e105323de69cd890bbd8f8bd82b56 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -174,6 +174,11 @@ CHANGES
     wasn't implementing the sort properly, replaced
     with the existing sort algorithm
 
+  - [bug] the "name" of a column-level CHECK constraint,
+    if present, is now rendered in the CREATE TABLE 
+    statement using "CONSTRAINT <name> CHECK <expression>". 
+    [ticket:2305]
+
 - pyodbc
    - [bug] pyodbc-based dialects now parse the 
      pyodbc accurately as far as observed
index 7aee5da81869009e7c8dbd9cb2a5a46f42f3e2d5..f49c0d1e9417ca5686ee21b4a1cc8ef26ec3d4c6 100644 (file)
@@ -1511,7 +1511,11 @@ class DDLCompiler(engine.Compiled):
         return text
 
     def visit_column_check_constraint(self, constraint):
-        text = "CHECK (%s)" % constraint.sqltext
+        text = ""
+        if constraint.name is not None:
+            text += "CONSTRAINT %s " % \
+                        self.preparer.format_constraint(constraint)
+        text += "CHECK (%s)" % constraint.sqltext
         text += self.define_constraint_deferrability(constraint)
         return text
 
index db7271e055f4bc597c4c9934b5d6e71ab55c1d1b..79a32d44ba627bbbc7e876bef9f3fa4302a14b11 100644 (file)
@@ -295,6 +295,15 @@ class ConstraintCompilationTest(fixtures.TestBase, AssertsCompiledSQL):
         assert 'NOT DEFERRABLE' not in sql
         assert 'INITIALLY DEFERRED' in sql
 
+    def test_column_level_ck_name(self):
+        t = Table('tbl', MetaData(),
+            Column('a', Integer, CheckConstraint("a > 5", name="ck_a_greater_five"))
+        )
+        self.assert_compile(
+            schema.CreateTable(t),
+            "CREATE TABLE tbl (a INTEGER CONSTRAINT "
+            "ck_a_greater_five CHECK (a > 5))"
+        )
     def test_deferrable_pk(self):
         factory = lambda **kw: PrimaryKeyConstraint('a', **kw)
         self._test_deferrable(factory)
@@ -312,7 +321,9 @@ class ConstraintCompilationTest(fixtures.TestBase, AssertsCompiledSQL):
 
         self.assert_compile(
             schema.CreateTable(t),
-            "CREATE TABLE tbl (a INTEGER, b INTEGER, FOREIGN KEY(b) REFERENCES tbl (a) DEFERRABLE INITIALLY DEFERRED)",
+            "CREATE TABLE tbl (a INTEGER, b INTEGER, "
+            "FOREIGN KEY(b) REFERENCES tbl "
+            "(a) DEFERRABLE INITIALLY DEFERRED)",
         )
 
     def test_deferrable_unique(self):