Anonymous avatar Anonymous committed 33692ba

[svn r120] Fix for #69: Make keep_default work for ForeignKeys.

Comments (0)

Files changed (2)

             # Now, drop the default if we need to
             if not keep_default and field.default:
                 field.default = NOT_PROVIDED
-                self.alter_column(table_name, name, field)
+                self.alter_column(table_name, name, field, explicit_name=False)
 
     alter_string_set_type = 'ALTER COLUMN %(column)s TYPE %(type)s'
     alter_string_set_null = 'ALTER COLUMN %(column)s DROP NOT NULL'
     alter_string_drop_null = 'ALTER COLUMN %(column)s SET NOT NULL'
     allows_combined_alters = True
 
-    def alter_column(self, table_name, name, field):
+    def alter_column(self, table_name, name, field, explicit_name=True):
         """
         Alters the given column name so it will match the given field.
         Note that conversion between the two by the database must be possible.
+        Will not automatically add _id by default; to have this behavour, pass
+        explicit_name=False.
 
         @param table_name: The name of the table to add the column to
         @param name: The name of the column to alter
             field.south_init()
 
         qn = connection.ops.quote_name
+        
+        # Add _id or whatever if we need to
+        if not explicit_name:
+            field.set_attributes_from_name(name)
+            name = field.column
 
         # First, change the type
         params = {
         db.start_transaction()
         # Add a column
         db.add_column("test4", "add1", models.IntegerField(default=3), keep_default=False)
+        # Add a FK with keep_default=False (#69)
+        User = db.mock_model(model_name='User', db_table='auth_user', db_tablespace='', pk_field_name='id', pk_field_type=models.AutoField, pk_field_args=[], pk_field_kwargs={})
+        db.add_column("test4", "user", models.ForeignKey(User), keep_default=False)
         
         db.rollback_transaction()
         db.delete_table("test4")
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.