Commits

and...@69d324d9-c39d-4fdc-8679-7745eae9e2c8  committed 7291a54

Fix from #61: uppercase column names for indexes. Added a few tests, and slightly tweaked the patch to work under MySQL.

  • Participants
  • Parent commits 1e1054e

Comments (0)

Files changed (2)

File db/generic.py

         """
         Generates a full SQL statement to add a foreign key constraint
         """
+        qn = connection.ops.quote_name
         constraint_name = '%s_refs_%s_%x' % (from_column_name, to_column_name, abs(hash((from_table_name, to_table_name))))
         return 'ALTER TABLE %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)%s;' % (
-            from_table_name,
-            truncate_name(constraint_name, connection.ops.max_name_length()),
-            from_column_name,
-            to_table_name,
-            to_column_name,
+            qn(from_table_name),
+            qn(truncate_name(constraint_name, connection.ops.max_name_length())),
+            qn(from_column_name),
+            qn(to_table_name),
+            qn(to_column_name),
             connection.ops.deferrable_sql() # Django knows this
         )
 
         """
         Generates a create index statement on 'table_name' for a list of 'column_names'
         """
+        qn = connection.ops.quote_name
         if not column_names:
             print "No column names supplied on which to create an index"
             return ''
         qn = connection.ops.quote_name
         return 'CREATE %sINDEX %s ON %s (%s)%s;' % (
             unique and 'UNIQUE ' or '',
-            index_name,
-            table_name,
+            qn(index_name),
+            qn(table_name),
             ','.join([qn(field) for field in column_names]),
             tablespace_sql
         )
         index naming function which relies on column names.
         """
         name = self.create_index_name(table_name, column_names)
-        sql = self.drop_index_string % {"index_name": name, "table_name": table_name}
+        qn = connection.ops.quote_name
+        sql = self.drop_index_string % {"index_name": qn(name), "table_name": qn(table_name)}
         self.execute(sql)
 
     drop_index = alias('delete_index')
             pass
         db.rollback_transaction()
     
+    def test_foreign_keys(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=[])
+        cursor = connection.cursor()
+        db.start_transaction()
+        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.rollback_transaction()
+    
     def test_rename(self):
         """
         Test column renaming
     def test_dry_rename(self):
         """
         Test column renaming while --dry-run is turned on (should do nothing)
+        See ticket #65
         """
         cursor = connection.cursor()
         db.create_table("test2", [('spam', models.BooleanField(default=False))])
         Test the index operations
         """
         db.create_table("test3", [
-            ('spam', models.BooleanField(default=False)),
+            ('SELECT', models.BooleanField(default=False)),
             ('eggs', models.IntegerField()),
         ])
         db.start_transaction()
         # Add an index on that column
-        db.create_index("test3", ["spam"])
+        db.create_index("test3", ["SELECT"])
         # Add another index on two columns
-        db.create_index("test3", ["spam", "eggs"])
+        db.create_index("test3", ["SELECT", "eggs"])
         # Delete them both
-        db.delete_index("test3", ["spam"])
-        db.delete_index("test3", ["spam", "eggs"])
+        db.delete_index("test3", ["SELECT"])
+        db.delete_index("test3", ["SELECT", "eggs"])
         db.rollback_transaction()
         db.delete_table("test3")