Django + South - migration can't cast the column type - wants me to use "USING" Django + South - migration can't cast the column type - wants me to use "USING" postgresql postgresql

Django + South - migration can't cast the column type - wants me to use "USING"


This was my dirty hack:

  1. Manually added a migraton for removing the offendig field i.e

migrations.RemoveField( model_name='name_of_the_model', name='offending_field', )2. Then I changed the operation of the complaining migration from AlterField to AddField i.e

migrations.AddField(        model_name='name_of_model',        name='name_of_field',        field=models.ForeignKey(blank=True, null=True, to='ForeignKeyModel'),        preserve_default=False,    ),

```


You have to substitute original db.alter_column with a db.execute:

# Changing field 'MyTable.associated'# db.alter_column(u'data_mytable', 'associated', #     self.gf('django.db.models.fields.IntegerField')()# )db.execute(    'ALTER TABLE "data_mytable" '    'ALTER COLUMN "associated" DROP DEFAULT, '    'ALTER COLUMN "associated" DROP NOT NULL, '    'ALTER COLUMN "associated" TYPE INTEGER '    'USING '    'CASE '    '  WHEN FALSE THEN 0 ELSE 1 '    'END') 

Of course, WHEN condition may change depending of the associated original type.
See also this link.