Rename column names with django-south

I check it about every one and a half years.

Premise

--Since 1.7, south has been incorporated into django, but for some reason I'm using 1.6 or less. ――It is assumed that the schema that contains the data already exists in the DBMS. --It is assumed that you have added south to ʻINSTALL_APPS`. --This is the procedure assuming that you want to change the desc column of the division table to the _desc column.

Work procedure

$ python manage.py syncdb
$ python manage.py schemamigration app --initial
$ python manage.py migrate --list
$ python manage.py migrate app --fake 0001  #Fake because there is already a table
$ python manage.py datamigration app underscore  #The underscore part can be anything

Hitting datamigration will generate a file called 0002_underscore.py in the migrations directory. Add the code "What changes to make" to this file. This time I want to rename it, so I will call the method rename_column. Only two lines in two places, forwards and backwards, are actually added.

class Migration(DataMigration):

    def forwards(self, orm):
        "Write your forwards methods here."
        db.rename_column('app_division', 'desc', '_desc')  # <-here

    def backwards(self, orm):
        "Write your backwards methods here."
        db.rename_column('app_division', '_desc', 'desc') # <-here

これが書けたらまたmanage.py

$ python manage.py migrate app 0002_underscore
$ sqlite3 nannrakano.db '.s' | grep app_division  #Verification

Recommended Posts

Rename column names with django-south
Replace column names / values with pandas dataframe
Get one column from DataFrame with DataFrame