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?
当我对模型进行更改时(仅在开发期间添加列!),Django不会发出任何ALTER TABLE语句来更新数据库。有没有办法实现或解决这个问题? - 其他然后手动添加列?
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.
注意我并不是真的在寻找一个完全迁移的解决方案,只是让我在路上添加列时可以继续编码。
5 个解决方案
#2
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):
使用python manage.py sqlclear YOURAPP与dumpdata和loaddata(fish2000的简化版本,也使用特定的应用程序):
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
#3
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.
对不起,这有点晚了,但我想我会在这里发帖,以防其他人遇到这个问题。如果您仍在开发中并且所有数据都是虚拟数据(意味着您不想保留任何数据),那么您所要做的就是删除数据库并再次运行syncdb。
#4
4
Check out evolution http://code.google.com/p/django-evolution/
查看进化http://code.google.com/p/django-evolution/
#5
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:
... bash函数的作用是:
- 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)
- 备份和删除二进制数据库文件(在这种情况下为SQLite,删除相关文件相对容易)
- 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)
- (可选)修复db文件的perms(在我的情况下,dropbox可以搞定)
- Repopulates the new DB from the last fixture
- 从最后一个夹具重新填充新DB
- (optional) restarts apache
- (可选)重启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.
在这些情况下,我运行命令,编辑models.py文件,删除DB文件并重新加载最后一个夹具。
Obviously, I don't do this on a production install, nor I would not recommend that.
显然,我不会在生产安装上执行此操作,也不建议这样做。
#1
#2
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):
使用python manage.py sqlclear YOURAPP与dumpdata和loaddata(fish2000的简化版本,也使用特定的应用程序):
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
#3
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.
对不起,这有点晚了,但我想我会在这里发帖,以防其他人遇到这个问题。如果您仍在开发中并且所有数据都是虚拟数据(意味着您不想保留任何数据),那么您所要做的就是删除数据库并再次运行syncdb。
#4
4
Check out evolution http://code.google.com/p/django-evolution/
查看进化http://code.google.com/p/django-evolution/
#5
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:
... bash函数的作用是:
- 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)
- 备份和删除二进制数据库文件(在这种情况下为SQLite,删除相关文件相对容易)
- 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)
- (可选)修复db文件的perms(在我的情况下,dropbox可以搞定)
- Repopulates the new DB from the last fixture
- 从最后一个夹具重新填充新DB
- (optional) restarts apache
- (可选)重启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.
在这些情况下,我运行命令,编辑models.py文件,删除DB文件并重新加载最后一个夹具。
Obviously, I don't do this on a production install, nor I would not recommend that.
显然,我不会在生产安装上执行此操作,也不建议这样做。