How can I automatically let syncdb add a column (no full migration needed)
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 col开发者_JS百科umns 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.
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
Use a migration tool such as South.
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.
Check out evolution http://code.google.com/p/django-evolution/
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:
- Dumps the database out to a fixture, named with a date/time stamp
- Backs up and deletes the binary database file (SQLite in this case, which it's comparatively easy to delete the file in question)
- Resyncs the the DB from models (regenerating the binary DB file)
- (optional) Fixes the DB files' perms (which dropbox can screw with, in my case here)
- Repopulates the new DB from the last fixture
- (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.
精彩评论