Django + South - Migration cannot use column type - I need to use "USE",

I get this error when I try to run ./manage.py migrate my_app :

django.db.utils.ProgrammingError: the "related" column cannot be automatically added for integer input
TIP. Specify a USING expression to perform the conversion.

That sounds like great advice, but I don’t know exactly what the South wants from me. Do I have to manually edit the automatically generated python migration file? If so, how - I do not see standard SQL in this file. Instead, I see the following:

 # Changing field 'MyTable.associated' db.alter_column(u'data_mytable', 'associated', self.gf('django.db.models.fields.IntegerField')() ) 

Also, why didn't the South come up to this and add the USING automatically?

+6
source share
2 answers

This was my dirty hack:

  • Manually added hyphenation to remove offendig ie field

migrations.RemoveField( model_name='name_of_the_model', name='offending_field', ) 2. Then I changed the complaint migration operation from AlterField to AddField ie

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

`` ``

+6
source

You should replace the original db.alter_column with 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, the WHEN condition may vary depending on the type of original associated .
See also link.

+2
source

All Articles