[Fixed]-How can I automatically let syncdb add a column (no full migration needed)

20👍

Use a migration tool such as South.

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

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

4👍

👤Ryan

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.

Leave a comment