1

How to do syncdb in django 1.4.2? i.e. having data in database, how to load the models again when the data schema is updated?

Thanks in advance

cck
  • 502
  • 4
  • 17
  • If you already have data in the database and have made changes to the models and want to update the database schema according to your changes in models while still keeping the data, you'll need to look into south migrations (https://south.readthedocs.org/en/latest/) – Amyth May 22 '15 at 03:44
  • 1
    The newer versions of django have the south module integrated by default. For your version of django, you'd need to install the south module and put it in your `INSTALLED_APPS`. – Amyth May 22 '15 at 03:47
  • Django 1.4 does support migrations. I had this source to verify, will have to download the version and check it again because the docs are not giving much info. http://stackoverflow.com/questions/10834237/django-syncdb-and-migrate – Arpit Goyal May 22 '15 at 03:56
  • @ArpitGoyal Look at the django migrations documentation https://docs.djangoproject.com/en/1.8/topics/migrations/. The documentation is available for django > 1.7 as south was officially included in django in version 1.7 – Amyth May 22 '15 at 04:13
  • @ArpitGoyal South has de-facto been a part of Django for a long time, but it was a separate package. 1.7 introduced a brand new migration system into Django core. – knbk May 22 '15 at 12:35
  • @Amyth "...have the south module integrated" is - I'm nitpicking here - not exactly true. The migration system in 1.7 has been made from scratch by Andrew Godwin, the creator of South. Though it was inspired by his previous work on South, it is very different in many aspects. – knbk May 22 '15 at 12:36

2 Answers2

6

As you're using an older version of django you'll need to install the South module and make migrations for your apps.

To install South, you can use pip or easy_install

pip install South

Once you have the south module installed put it in your django projects settings' INSTALLED_APPS

INSTALLED_APPS = (
...
"south"
)

Then you'll need to make initial migrations first for your apps. So for an app named example you can run the command:

python manage.py makemigrations example --initial
python manage.py migrate

After the initial migration creation you make changes to your models and then make the new migrations and apply them.

python manage.py makemigrations example --auto
python manage.py migrate
Amyth
  • 32,527
  • 26
  • 93
  • 135
3

Thanks Amyth for the hints.
btw the commands is a bit different, i will post a 10x tested result here.

Using south
1. setup the model

python manage.py schemamigration models --initial

  1. dump data if you have to

python manage.py dumpdata -e contenttypes -e auth.Permission --natural > data.json

  1. syncdb

python manage.py syncdb
python manage.py migrate models

  1. load the data back into the db

python manage.py loaddata data.json

  1. Afterwards, you may use

python manage.py schemamigration models --auto
python manage.py migrate models

after every change you made in the models schema

A few notes
1. Unloading the database and reload it is essential, because if not doing so the first migration will tell you already have those models.
2. The -e contenttypes -e auth.Permission --natural parameter in dumpdata is essential otherwise exception will be thrown when doing loaddata.

Rahul Gupta
  • 46,769
  • 10
  • 112
  • 126
cck
  • 502
  • 4
  • 17