and...@69d324d9-c39d-4fdc-8679-7745eae9e2c8  committed 6cb1776

Fix for #69: Make keep_default work for ForeignKeys.

  • Participants
  • Parent commits 1d031a3
  • Branches default

Comments (0)

Files changed (2)

File db/

             # 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
         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 = {
         # 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)