1. Andrew Godwin
  2. south

Commits

Andrew Godwin  committed bf2f3ae Merge

Merged in maxirobaina/south (pull request #152)

Fixes for double index creation

  • Participants
  • Parent commits 7ce200e, 74993d6
  • Branches default

Comments (0)

Files changed (3)

File south/db/firebird.py

View file
 
     @generic.invalidate_table_constraints
     def create_table(self, table_name, fields):
-        qn = self.quote_name(table_name)
         columns = []
         autoinc_sql = ''
 
         for field_name, field in fields:
+            # avoid default values in CREATE TABLE statements (#925)
+            field._suppress_default = True
+            
             col = self.column_sql(table_name, field_name, field)
             if not col:
                 continue
                 field_name = field.db_column or field.column
                 autoinc_sql = connection.ops.autoinc_sql(table_name, field_name)
 
-        sql = 'CREATE TABLE %s (%s);' % (qn, ', '.join([col for col in columns]))
-        self.execute(sql)
+        self.execute(self.create_table_sql % {
+            "table": self.quote_name(table_name),
+            "columns": ', '.join([col for col in columns if col]),
+        })
+        
         if autoinc_sql:
             self.execute(autoinc_sql[0])
             self.execute(autoinc_sql[1])
             for stmt in field.post_create_sql(no_style(), table_name):
                 self.add_deferred_sql(stmt)
 
-        # In 1.2 and above, you have to ask the DatabaseCreation stuff for it.
-        # This also creates normal indexes in 1.1.
-        if hasattr(self._get_connection().creation, "sql_indexes_for_field"):
-            # Make a fake model to pass in, with only db_table
-            model = self.mock_model("FakeModelForGISCreation", table_name)
-            for stmt in self._get_connection().creation.sql_indexes_for_field(model, field, no_style()):
-                self.add_deferred_sql(stmt)
+        # Avoid double index creation (#1317)
+        # Firebird creates an index implicity for each foreign key field 
+        # sql_indexes_for_field tries to create an index for that field too
+        if not field.rel:
+            # In 1.2 and above, you have to ask the DatabaseCreation stuff for it.
+            # This also creates normal indexes in 1.1.
+            if hasattr(self._get_connection().creation, "sql_indexes_for_field"):
+                # Make a fake model to pass in, with only db_table
+                model = self.mock_model("FakeModelForGISCreation", table_name)
+                for stmt in self._get_connection().creation.sql_indexes_for_field(model, field, no_style()):
+                    self.add_deferred_sql(stmt)
 
         if sql:
             return sql % sqlparams

File south/tests/__init__.py

View file
 if not skiptest:
     from south.tests.db import *
     from south.tests.db_mysql import *
+    from south.tests.db_firebird import *
     from south.tests.logic import *
     from south.tests.autodetection import *
     from south.tests.logger import *

File south/tests/db_firebird.py

View file
+from django.db import models
+
+from south.db import db
+from south.tests import unittest, skipIf, skipUnless
+
+
+class FirebirdTests(unittest.TestCase):
+
+    """
+    Tests firebird related issues
+    """
+
+    def setUp(self):
+        print('=' * 80)
+        print('Begin Firebird test')
+
+    def tearDown(self):
+        print('End Firebird test')
+        print('=' * 80)
+
+    def test_firebird_double_index_creation_1317(self):
+        """
+        Tests foreign key creation, especially uppercase (see #61)
+        """
+        Test = db.mock_model(model_name='Test',
+            db_table='test5a',
+            db_tablespace='',
+            pk_field_name='ID',
+            pk_field_type=models.AutoField,
+            pk_field_args=[]
+        )
+        db.create_table("test5a", [('ID', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True))])
+        db.create_table("test5b", [
+            ('id', models.AutoField(verbose_name='ID', primary_key=True, auto_created=True)),
+            ('UNIQUE', models.ForeignKey(Test)),
+        ])
+        db.execute_deferred_sql()
+