views:

59

answers:

3

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.

+4  A: 

Use a migration tool such as South.

Ignacio Vazquez-Abrams
@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
A: 

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 DB file and reload the last fixture.

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

fish2000
A: 

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

Ryan