我正在使用 Django 使用 MySQL 构建网站。现在我正在学习,所以我需要经常更改模型,所以我希望所有表都被清除并创建新表。
但 syncdb
不会触及现有表。有没有更好的方法来处理这个问题?
最佳答案
如果你不关心数据:
最好的方法是删除数据库并再次运行 syncdb
。或者你可以运行:
对于 Django >= 1.5
python manage.py flush
对于 Django <1.5
python manage.py reset appname
(您可以在命令末尾添加 --no-input
以跳过交互式提示。)
如果您确实关心数据:
来自文档:
syncdb will only create tables for models which have not yet been installed. It will never issue ALTER TABLE statements to match changes made to a model class after installation. Changes to model classes and database schemas often involve some form of ambiguity and, in those cases, Django would have to guess at the correct changes to make. There is a risk that critical data would be lost in the process.
If you have made changes to a model and wish to alter the database tables to match, use the sql command to display the new SQL structure and compare that to your existing table schema to work out the changes.
https://docs.djangoproject.com/en/dev/ref/django-admin/
引用:常见问题解答 - https://docs.djangoproject.com/en/dev/faq/models/#if-i-make-changes-to-a-model-how-do-i-update-the-database
人们也推荐 South (http://south.aeracode.org/docs/about.html#key-features),但我没有尝试过。
关于python - 在 Django 中使用 manage.py 从 CLI 清除数据库的最简单方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6485106/