]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Bug Fix: Stop generating bad sql if an empty UniqueConstraint() is given
authordonkopotamus <derek.harland@finq.co.nz>
Thu, 16 Jan 2014 22:00:24 +0000 (11:00 +1300)
committerdonkopotamus <derek.harland@finq.co.nz>
Thu, 16 Jan 2014 22:00:24 +0000 (11:00 +1300)
lib/sqlalchemy/dialects/mssql/base.py
lib/sqlalchemy/sql/compiler.py
test/sql/test_constraints.py

index c024b62304dc2e7799fde51f88bef91ce5e956b2..6c942b270a0eb2f1b98f2a11b55d385f9581672a 100644 (file)
@@ -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 " % \
index ade3c623af3e242222d916ac71226ada104ee5f8..5c5bfad55136b688e00e45e7793a665a6af6ab21 100644 (file)
@@ -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 " % \
index 393bcd4480295ff27f9b008b5df2d4814178d42d..cb4b73ec8b9804846ace3b7a117166e9c3becb3a 100644 (file)
@@ -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),