12

I'm using Django ( < v1.7), with SQLite3 as the database engine.

I am trying to add a new field to an already existing model's class. This class already has data associated to it.

manage.py syncdb is not adding the new fields to the db.

Is there was a way to:

  1. Add the new Field to the already existing class, and make sure a db entry is allocated to it?

  2. Populate this new field with an empty string for all the already existing db entries?

Dio Phung
  • 5,944
  • 5
  • 37
  • 55
Rami_H
  • 401
  • 1
  • 4
  • 15

3 Answers3

22

This answer is still getting visibility but is outdated. Since 1.7 Django ships with a built-in migration system, written by the same author as South, and has deprecated syncdb though it will still work.

You will simply need to run a few commands to automatically add new columns:

python manage.py makemigrations  
python manage.py migrate

It will be useful to understand what's happening under the hood, but those are the basics. Please ask new questions to get answers on 1.7 and migrations if you are still reading this old post.


For django < 1.7

syncdb will not add any new columns. See http://docs.djangoproject.com/en/dev/ref/django-admin/#syncdb

You will have to add them manually. For example,. replace <> with relevant info:

python manage.py dbshell
ALTER TABLE <appname_modelname> ADD COLUMN <column_type> DEFAULT '';

You can see what Django might have done to generate columns on a fresh syncdb by using:

python manage.py sqlall app_name

and copying and pasting ALTER TABLE statements from there.

Otherwise, you can look into third-party apps like Django-South which are database migration tools.

Yuji 'Tomita' Tomita
  • 115,817
  • 29
  • 282
  • 245
  • 1
    +1 simple and works for all databases, particularly with the `sqlall` advice. – Seth Oct 10 '12 at 22:28
  • Though this works a better approach is using south as it creates migration scripts. – poorva Dec 17 '14 at 09:39
  • @poorva I did reference south in my post, but actually django 1.7+ has built in migrations and south is no longer needed. – Yuji 'Tomita' Tomita Dec 17 '14 at 23:29
  • @Yuji'Tomita'Tomita : a very nice answer ! but can you also tell me that If I have to add some new tables by creating new classes in models.py, which already has some classes and corresponding tables with data, then how can I do that without disturbing the previous data? If the commands stated above are to be used then some warnings are thrown as soon as I type python manage.py makemigrations my-app-label and no new tables are created. – POOJA GUPTA Jun 06 '15 at 07:25
11

Install south in your django and you can easily handle the existing tables. check this

If you really want to use Django-South, install it on your django, after adding your new fields to your existing model run

python manage.py schemamigration --initial

It will create a file in your project app. then,

python manage.py migrate

thats it your table is altered.

shivg
  • 742
  • 1
  • 8
  • 28
  • thx I think that it is what I was looking for. Yuji's method for manually modifying the db is also interesting. I will test both method first thing in the morning! – Rami_H Mar 01 '11 at 05:07
  • South is not relevant anymore. Please update your answer – rrawat Apr 22 '17 at 13:28
0

There are other options of migration apps (although South is the most used).

I have used django-evolution for my projects and it was very easy to install and start using.

South seems to be more complete, but for simpler tasks, django-evolution may be suitable.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Renne Rocha
  • 1,235
  • 1
  • 9
  • 10