From: Mike Bayer Date: Sun, 4 Dec 2011 18:46:45 +0000 (-0500) Subject: - [bug] the "name" of a column-level CHECK constraint, X-Git-Tag: rel_0_7_4~32 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=133a0b2460f4c71692795ebbe52f9e521bb8782c;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - [bug] the "name" of a column-level CHECK constraint, if present, is now rendered in the CREATE TABLE statement using "CONSTRAINT CHECK ". [ticket:2305] --- diff --git a/CHANGES b/CHANGES index 12d4826ef7..b9bdfb1521 100644 --- 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 CHECK ". + [ticket:2305] + - pyodbc - [bug] pyodbc-based dialects now parse the pyodbc accurately as far as observed diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py index 7aee5da818..f49c0d1e94 100644 --- a/lib/sqlalchemy/sql/compiler.py +++ b/lib/sqlalchemy/sql/compiler.py @@ -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 diff --git a/test/sql/test_constraints.py b/test/sql/test_constraints.py index db7271e055..79a32d44ba 100644 --- a/test/sql/test_constraints.py +++ b/test/sql/test_constraints.py @@ -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):