Commits

Andrew Godwin committed 85d8f63

Fix column naming issues for add/delete fields

  • Participants
  • Parent commits 510ae57
  • Branches 0.7

Comments (0)

Files changed (2)

File south/creator/actions.py

 import datetime
 
 from django.db.models.fields.related import RECURSIVE_RELATIONSHIP_CONSTANT
-from django.db.models.fields import FieldDoesNotExist
+from django.db.models.fields import FieldDoesNotExist, NOT_PROVIDED
 
 from south import modelsinspector
 from south.creator.freezer import remove_useless_attributes, model_key
     
     BACKWARDS_TEMPLATE = '''
         # Deleting field '%(model_name)s.%(field_name)s'
-        db.delete_column(%(table_name)r, %(field_name)r)'''
+        db.delete_column(%(table_name)r, %(field_column)r)'''
     
     def __init__(self, model, field, field_def):
         self.model = model
-        self.field_name = field
+        self.field = field
         self.field_def = field_def
         
         # See if they've made a NOT NULL column but also have no default (far too common)
-        is_null = self.field_def[2].get("null", False)
-        default = self.field_def[2].get("default", None)
+        is_null = self.field.null
+        default = bool(self.field.default) and (self.field.default is not NOT_PROVIDED)
+        
         if not is_null and not default:
             # Oh dear. Ask them what to do.
             print " ? The field '%s.%s' does not have a default specified, yet is NOT NULL." % (
                 self.model._meta.object_name,
-                self.field_name,
+                self.field.name,
             )
             print " ? Since you are adding or removing this field, you MUST specify a default"
             print " ? value to use for existing rows. Would you like to:"
     def console_line(self):
         "Returns the string to print on the console, e.g. ' + Added field foo'"
         return " + Added field %s on %s.%s" % (
-            self.field_name,
+            self.field.name,
             self.model._meta.app_label, 
             self.model._meta.object_name,
         )
         return self.FORWARDS_TEMPLATE % {
             "model_name": self.model._meta.object_name,
             "table_name": self.model._meta.db_table,
-            "field_name": self.field_name,
+            "field_name": self.field.name,
+            "field_column": self.field.column,
             "field_def": self.triple_to_def(self.field_def),
         }
 
         return self.BACKWARDS_TEMPLATE % {
             "model_name": self.model._meta.object_name,
             "table_name": self.model._meta.db_table,
-            "field_name": self.field_name,
+            "field_name": self.field.name,
+            "field_column": self.field.column,
         }
     
     
     def console_line(self):
         "Returns the string to print on the console, e.g. ' + Added field foo'"
         return " - Deleted field %s on %s.%s" % (
-            self.field_name,
+            self.field.name,
             self.model._meta.app_label, 
             self.model._meta.object_name,
         )

File south/creator/changes.py

                 # Find fields that have vanished.
                 for fieldname in old_fields:
                     if fieldname not in new_fields:
-                        if isinstance(self.old_orm[key+":"+fieldname], models.ManyToManyField):
-                            yield ("DeleteM2M", {"model": self.old_orm[key], "field": fieldname})
+                        if isinstance(self.old_orm[key + ":" + fieldname], models.ManyToManyField):
+                            yield ("DeleteM2M", {"model": self.old_orm[key], "field": self.old_orm[key + ":" + fieldname]})
                         else:
-                            yield ("DeleteField", {"model": self.old_orm[key], "field": fieldname, "field_def": old_fields[fieldname]})
+                            yield ("DeleteField", {"model": self.old_orm[key], "field": self.old_orm[key + ":" + fieldname], "field_def": old_fields[fieldname]})
                 
                 # And ones that have appeared
                 for fieldname in new_fields:
                     if fieldname not in old_fields:
                         if isinstance(self.current_model_from_key(key)._meta.get_field_by_name(fieldname)[0], models.ManyToManyField):
-                            yield ("AddM2M", {"model": self.old_orm[key], "field": fieldname})
+                            yield ("AddM2M", {"model": self.current_model_from_key[key], "field": self.current_field_from_key(key, fieldname)})
                         else:
-                            yield ("AddField", {"model": self.current_model_from_key(key), "field": fieldname, "field_def": new_fields[fieldname]})
+                            yield ("AddField", {"model": self.current_model_from_key(key), "field": self.current_field_from_key(key, fieldname), "field_def": new_fields[fieldname]})
                 
                 # For the ones that exist in both models, see if they were changed
                 for fieldname in set(old_fields).intersection(set(new_fields)):