[ACCEPTED]-Adding fields to an already existing database for Django (version < 1.7)-database-migration
This answer is still getting visibility 17 but is outdated. Since 1.7 Django ships 16 with a built-in migration system, written by the same author 15 as
South, and has deprecated
syncdb though it will 14 still work.
You will simply need to run a 13 few commands to automatically add new columns:
python manage.py makemigrations python manage.py migrate
It 12 will be useful to understand what's happening 11 under the hood, but those are the basics. Please 10 ask new questions to get answers on 1.7 9 and migrations if you are still reading 8 this old post.
For django < 1.7
syncdb will not add any new columns. See
You 7 will have to add them manually. For example,. replace 6
<> with relevant info:
python manage.py dbshell ALTER TABLE <appname_modelname> ADD COLUMN <column_type> DEFAULT '';
You can see what Django 5 might have done to generate columns on a 4 fresh
syncdb by using:
python manage.py sqlall app_name
and copying and pasting 3
ALTER TABLE statements from there.
Otherwise, you can 2 look into third-party apps like Django-South which are 1 database migration tools.
Install south in your django and you can 8 easily handle the existing tables. check 7 this
If you really want to use Django-South, install 6 it on your django, after adding your new 5 fields to your existing model run
python 4 manage.py schemamigration --initial
It will 3 create a file in your project app. then,
python 2 manage.py migrate
thats it your table is 1 altered.
There are other options of migration apps 5 (although South is the most used).
I have 4 used django-evolution for my projects and it was very easy 3 to install and start using.
South seems to 2 be more complete, but for simpler tasks, django-evolution 1 may be suitable.
More Related questions