From cf8e5e3cf5b0e1be05a611c8828690acfcd2b9fa Mon Sep 17 00:00:00 2001 From: donkopotamus Date: Fri, 17 Jan 2014 11:00:24 +1300 Subject: [PATCH] Bug Fix: Stop generating bad sql if an empty UniqueConstraint() is given --- lib/sqlalchemy/dialects/mssql/base.py | 2 ++ lib/sqlalchemy/sql/compiler.py | 2 ++ test/sql/test_constraints.py | 22 ++++++++++++++++++++++ 3 files changed, 26 insertions(+) diff --git a/lib/sqlalchemy/dialects/mssql/base.py b/lib/sqlalchemy/dialects/mssql/base.py index c024b62304..6c942b270a 100644 --- a/lib/sqlalchemy/dialects/mssql/base.py +++ b/lib/sqlalchemy/dialects/mssql/base.py @@ -1069,6 +1069,8 @@ class MSDDLCompiler(compiler.DDLCompiler): return text def visit_unique_constraint(self, constraint): + if len(constraint) == 0: + return '' text = "" if constraint.name is not None: text += "CONSTRAINT %s " % \ diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py index ade3c623af..5c5bfad551 100644 --- a/lib/sqlalchemy/sql/compiler.py +++ b/lib/sqlalchemy/sql/compiler.py @@ -2511,6 +2511,8 @@ class DDLCompiler(Compiled): return preparer.format_table(table) def visit_unique_constraint(self, constraint): + if len(constraint) == 0: + return '' text = "" if constraint.name is not None: text += "CONSTRAINT %s " % \ diff --git a/test/sql/test_constraints.py b/test/sql/test_constraints.py index 393bcd4480..cb4b73ec8b 100644 --- a/test/sql/test_constraints.py +++ b/test/sql/test_constraints.py @@ -544,6 +544,28 @@ class ConstraintCompilationTest(fixtures.TestBase, AssertsCompiledSQL): "FOREIGN KEY(foo_bar) REFERENCES foo (bar))" ) + def test_empty_pkc(self): + # test that an empty primary key is ignored + metadata = MetaData() + tbl = Table('test', metadata, + Column('x', Integer, autoincrement=False), + Column('y', Integer, autoincrement=False), + PrimaryKeyConstraint()) + self.assert_compile(schema.CreateTable(tbl), + "CREATE TABLE test (x INTEGER, y INTEGER)" + ) + + def test_empty_uc(self): + # test that an empty constraint is ignored + metadata = MetaData() + tbl = Table('test', metadata, + Column('x', Integer, autoincrement=False), + Column('y', Integer, autoincrement=False), + UniqueConstraint()) + self.assert_compile(schema.CreateTable(tbl), + "CREATE TABLE test (x INTEGER, y INTEGER)" + ) + def test_deferrable_column_check(self): t = Table('tbl', MetaData(), Column('a', Integer), -- 2.47.3