Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I migrate existing CharField with data to IntegerField in Django

I have some models with CharFields in in my models.py.

e.g.

class MyModel(models.Model):
    item_port = models.CharField(max_length=50)`

I have realised that this was a little stupid for a port and changed them all to IntegerFields as follows:

class MyModel(models.Model):
    item_port = models.IntegerField(null=True)

I am using sqlite3 in my local dev system and a MySQL type database for deployment. Typically, I manufacture migrations for a new docker instance when deploying from scratch using:

python manage.py makemigrations and python manage.py migrate

However, I also have a record of all migrations made to my sqlite3 db in git for reference purposes.

My main question is, can I get away with simply doing a makemigrations/migrate from CharField to IntegerField with existing data in the database (which on client sites will be a MySQL type database OR do I need to do something like this manually?:

https://docs.djangoproject.com/en/1.9/topics/migrations/#data-migrations

Thanks very much.

like image 856
Rodent Avatar asked Nov 08 '22 15:11

Rodent


1 Answers

If you notice that item_port should be a IntergerField not a CharField you can run

./manage.py schemamigration myapp --auto --update

Refering to the South documentation Advanced Commands and Data Migrations

Hope this will help :)

like image 120
Babacar Gningue Avatar answered Nov 14 '22 21:11

Babacar Gningue