19

When I make a change to my models (only to add a column during development!), Django won't issue any ALTER TABLE statements to update the database. Is there a way to get this implemented or worked around? - other then adding the columns manually?


Note I'm not really looking for a full-migration solution, just something that lets me keep coding as I add columns on the way.

vdboor
  • 19,540
  • 11
  • 74
  • 91
  • 1
    possible duplicate of [What is your favorite solution for managing database migrations in django?](http://stackoverflow.com/questions/426378/what-is-your-favorite-solution-for-managing-database-migrations-in-django) – Daniel Roseman Oct 13 '10 at 14:03
  • ... among many other questions on the same subject. – Daniel Roseman Oct 13 '10 at 14:03
  • I've reworded it a bit. What I'm looking for is not a complete migration suite, but something to add columns quickly during development. E.g. `syncdb` sees a column is missing, and adds it automatically. – vdboor Oct 15 '10 at 11:36

5 Answers5

23

Use python manage.py sqlclear YOURAPP in conjunction with dumpdata and loaddata (simplified version of answer by fish2000, which also uses a specific app):

DB=/path/to/db.sqlite3
APP=YOURAPPNAME
tmpdate=$(date "+%Y%m%d-%H%M%S")

echo "+ Dumping django project database to fixture DUMP-${tmpdate}.json ..." &&\
python manage.py dumpdata $APP --format='json' --indent=4 --verbosity=1 > datadumps/DUMP-${tmpdate}.json &&\
echo '+ Backing up sqlite binary store...' &&\
cp $DB $DB.bk &&\
echo '+ Rebuilding database structure from model defs...' &&\
python manage.py sqlclear $APP &&\
echo "+ Reloading project data from fixture dump DUMP-${tmpdate}.json ..." &&\
python manage.py loaddata datadumps/DUMP-${tmpdate}.json
GreenAsJade
  • 14,002
  • 9
  • 54
  • 91
Alex Flint
  • 4,939
  • 7
  • 32
  • 67
  • If a django model has a column and a database doesn't - I get an error. Has this worked for anyone else? – Mikhail Feb 04 '13 at 03:55
  • 4
    In newer versions of django (e.g. 1.5) the command to use is python manage.py sqlclear instead of reset – tjb Jul 26 '13 at 12:36
20

Use a migration tool such as South.

Ignacio Vazquez-Abrams
  • 699,552
  • 132
  • 1,235
  • 1,283
  • @vbdoor: there is no native schema modification in the Django ORM. It must be done manually or with a utility such as South. – Andrew Sledge Oct 13 '10 at 14:57
  • 1
    Migrations appear to be on their way in Django 1.7: https://docs.djangoproject.com/en/dev/topics/migrations/ – Seth Feb 17 '14 at 21:27
5

Sorry, this is a little late but I thought I would post it here anyways in case anyone else is having this problem. If you are still in development and all of your data is dummy data (meaning you don't want to keep any of it), then all you have to do is delete the database and run syncdb again.

jerry
  • 2,515
  • 5
  • 36
  • 60
4

Check out evolution http://code.google.com/p/django-evolution/

Ryan
  • 1,015
  • 1
  • 10
  • 15
3

If you don't want to set up migrations – you may be able to use a trick like this:

export JANGY_PROJECT='/Users/fish/Dropbox/ost2/ost2'
export BPYTHON_INIT_SCRIPT='${JANGY_PROJECT}/utils/django_shell_imports.py'
export PYTHONPATH="${JANGY_PROJECT}:${PYTHONPATH}"

alias jangy="python manage.py"
alias bp="cd $JANGY_PROJECT && bpython --interactive $BPYTHON_INIT_SCRIPT"


function jangyfresh () {
    tmpdate=$(date "+%Y%m%d-%H%M%S") &&\
    cd $JANGY_PROJECT &&\
    echo "+ Dumping django project database to fixture DUMP-${tmpdate}.json ..." &&\
    python manage.py dumpdata --format='json' --indent=4 --verbosity=1 > datadumps/DUMP-${tmpdate}.json &&\
    echo '+ Backing up sqlite binary store and taking database offline...' &&\
    mv sqlite/data.db sqlite/data.db.bk &&\
    echo '+ Rebuilding database structure from model defs...' &&\
    python manage.py syncdb &&\
    echo '+ Graceful-restarting Apache...' &&\
    sudo apachectl graceful &&\
    echo '+ Enabling write access on new sqlite file...' &&\
    chmod a+rw sqlite/data.db &&\
    echo "+ Reloading project data from fixture dump DUMP-${tmpdate}.json ..." &&\
    python manage.py loaddata datadumps/DUMP-${tmpdate}.json &&\
    echo '+ Rebuilding project database structure...'
}

... which what that bash function does is:

  1. Dumps the database out to a fixture, named with a date/time stamp
  2. Backs up and deletes the binary database file (SQLite in this case, which it's comparatively easy to delete the file in question)
  3. Resyncs the the DB from models (regenerating the binary DB file)
  4. (optional) Fixes the DB files' perms (which dropbox can screw with, in my case here)
  5. Repopulates the new DB from the last fixture
  6. (optional) restarts apache

I use this during development to back things up and start from scratch – sometimes it works if you add a column, sometimes it'll complain about the newly defined model field not having a database column.

In these cases I run the command, edit the models.py file, delete the sqlite file and reload the last fixture.

Obviously, I don't do this on a production install, nor I would not recommend that.

fish2000
  • 3,985
  • 2
  • 37
  • 67