Commits

Andrew Godwin  committed 763a49d

Fix from #421, with test.

  • Participants
  • Parent commits 630dd2b

Comments (0)

Files changed (3)

File south/db/generic.py

             return field.db_type(connection=self._get_connection())
         except TypeError:
             return field.db_type()
+        
+    def _alter_set_defaults(self, field, name, params, sqls): 
+        "Subcommand of alter_column that sets default values (overrideable)"
+        # Next, set any default
+        if not field.null and field.has_default():
+            default = field.get_default()
+            sqls.append(('ALTER COLUMN %s SET DEFAULT %%s ' % (self.quote_name(name),), [default]))
+        else:
+            sqls.append(('ALTER COLUMN %s DROP DEFAULT' % (self.quote_name(name),), []))
 
     def alter_column(self, table_name, name, field, explicit_name=True, ignore_constraints=False):
         """
         # SQLs is a list of (SQL, values) pairs.
         sqls = [(self.alter_string_set_type % params, [])]
 
-        # Next, set any default
-        if not field.null and field.has_default():
-            default = field.get_default()
-            sqls.append(('ALTER COLUMN %s SET DEFAULT %%s ' % (self.quote_name(name),), [default]))
-        else:
-            sqls.append(('ALTER COLUMN %s DROP DEFAULT' % (self.quote_name(name),), []))
-
         # Next, nullity
         if field.null:
             sqls.append((self.alter_string_set_null % params, []))
         else:
             sqls.append((self.alter_string_drop_null % params, []))
 
+        # Next, set any default
+        self._alter_set_defaults(field, name, params, sqls)
+
         # Finally, actually change the column
         if self.allows_combined_alters:
             sqls, values = zip(*sqls)

File south/db/mysql.py

         if self._db_type_for_alter_column(field).upper() in ["BLOB", "TEXT", "LONGTEXT"]:
             field._suppress_default = True
         return field
+    
+    
+    def _alter_set_defaults(self, field, name, params, sqls):
+        """
+        MySQL does not support defaults on text or blob columns.
+        """
+        type = params['type']
+        print type
+    
+        if not (type.endswith('text') or type.endswith('blob')):
+            super(DatabaseOperations, self)._alter_set_defaults(field, name, params, sqls)

File south/tests/db.py

         # Change eggs to be a FloatField
         db.alter_column("test_alterc", "eggs", models.FloatField())
         db.delete_table("test_alterc")
-        
+    
+    def test_mysql_defaults(self):
+        """
+        Test MySQL default handling for BLOB and TEXT.
+        """
+        db.create_table("test_altermyd", [
+            ('spam', models.BooleanField(default=False)),
+            ('eggs', models.TextField()),
+        ])
+        # Change eggs to be a FloatField
+        db.alter_column("test_altermyd", "eggs", models.TextField(null=True))
+        db.delete_table("test_altermyd")
+    
     def test_alter_column_postgres_multiword(self):
         """
         Tests altering columns with multiple words in Postgres types (issue #125)