Commits

Andrew Godwin committed 510ae57

Fix foreign keys in xxxx_unique code creation.

  • Participants
  • Parent commits 9dc5791
  • Branches 0.7

Comments (0)

Files changed (2)

File south/creator/actions.py

     
     FORWARDS_TEMPLATE = '''
         # Adding field '%(model_name)s.%(field_name)s'
-        db.add_field(%(table_name)r, %(field_name)r, %(field_def)s)'''
+        db.add_column(%(table_name)r, %(field_name)r, %(field_def)s)'''
     
     BACKWARDS_TEMPLATE = '''
         # Deleting field '%(model_name)s.%(field_name)s'
-        db.delete_field(%(table_name)r, %(field_name)r)'''
+        db.delete_column(%(table_name)r, %(field_name)r)'''
     
     def __init__(self, model, field, field_def):
         self.model = model
     """
     
     FORWARDS_TEMPLATE = '''
-        # Adding unique constraint on '%(model_name)s', fields %(fields)s
+        # Adding unique constraint on '%(model_name)s', fields %(field_names)s
         db.create_unique(%(table_name)r, %(fields)r)'''
     
     BACKWARDS_TEMPLATE = '''
-        # Removing unique constraint on '%(model_name)s', fields %(fields)s
+        # Removing unique constraint on '%(model_name)s', fields %(field_names)s
         db.delete_unique(%(table_name)r, %(fields)r)'''
     
     def __init__(self, model, fields):
         return self.FORWARDS_TEMPLATE % {
             "model_name": self.model._meta.object_name,
             "table_name": self.model._meta.db_table,
-            "fields": self.fields,
+            "fields":  [field.column for field in self.fields],
+            "field_names":  [field.name for field in self.fields],
         }
 
     def backwards_code(self):
         return self.BACKWARDS_TEMPLATE % {
             "model_name": self.model._meta.object_name,
             "table_name": self.model._meta.db_table,
-            "fields": self.fields,
+            "fields": [field.column for field in self.fields],
+            "field_names":  [field.name for field in self.fields],
         }
 
 

File south/creator/changes.py

         app_label, model_name = key.split(".")
         return models.get_model(app_label, model_name)
     
+    def current_field_from_key(self, key, fieldname):
+        app_label, model_name = key.split(".")
+        return models.get_model(app_label, model_name)._meta.get_field_by_name(fieldname)[0]
+    
     def get_changes(self):
         """
         Returns the difference between the old and new sets of models as a 5-tuple:
                                 if old_triple[2]['unique'] == "False":
                                     yield ("AddUnique", {
                                         "model": self.current_model_from_key(key),
-                                        "fields": [fieldname],
+                                        "fields": [self.current_field_from_key(key, fieldname)],
                                     })
                                 else:
                                     yield ("DeleteUnique", {
                                         "model": self.old_orm[key],
-                                        "fields": [fieldname],
+                                        "fields": [self.old_orm[key + ":" + fieldname]],
                                     })
                             else:
                                 if new_triple[2]['unique'] == "False":
                                     yield ("DeleteUnique", {
                                         "model": self.old_orm[key],
-                                        "fields": [fieldname],
+                                        "fields": [self.old_orm[key + ":" + fieldname]],
                                     })
                                 else:
                                     yield ("AddUnique", {
                                         "model": self.current_model_from_key(key),
-                                        "fields": [fieldname],
+                                        "fields": [self.current_field_from_key(key, fieldname)],
                                     })
                 
                 ## See if the unique_togethers have changed
                     if item not in new_unique_together:
                         yield ("DeleteUnique", {
                             "model": self.old_orm[key],
-                            "fields": list(item),
+                            "fields": [self.old_orm[key + ":" + x] for x in item],
                         })
                 for item in new_unique_together:
                     if item not in old_unique_together:
                         yield ("AddUnique", {
                             "model": self.current_model_from_key(key),
-                            "fields": list(item),
+                            "fields": [self.current_field_from_key(key, x) for x in item],
                         })
 
     @classmethod