Python 在 Django 中重置 SQLite 数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37083445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reset SQLite database in Django
提问by InnisBrendan
I am trying to refactor a Django project. I renamed a couple apps and added a new one, as well as shuffled some models around. I want to clear my database and migrations and start fresh, but I am not sure how to accomplish this. Here's what I did:
我正在尝试重构一个 Django 项目。我重命名了几个应用程序并添加了一个新应用程序,并调整了一些模型。我想清除我的数据库和迁移并重新开始,但我不确定如何完成此操作。这是我所做的:
rm -r myapp/migrations // I ran this for all my apps
python manage.py flush
python manage.py makemigrations myapp // I ran this for all my apps
python manage.py migrate // This errors
I get an error:
我收到一个错误:
django.db.utils.OperationalError: table "myapp_mymodel" already exists
Can anyone tell me what I might be doing wrong?
谁能告诉我我可能做错了什么?
EDIT: What is the django command to delete all tables?did not work.
编辑:删除所有表的 django 命令是什么?不工作。
回答by vsd
Delete database and delete migration files (.py
and .pyc
) in migrations
directory of your app (don't delete __init__.py
file). Then run python manage.py makemigrations app
and python manage.py migrate
.
删除数据库并删除应用程序目录中的迁移文件(.py
和.pyc
)migrations
(不要删除__init__.py
文件)。然后运行python manage.py makemigrations app
和python manage.py migrate
。
回答by Daniel Vieira
I had the same issue, using Django 1.10, here is what I did, I deleted the database sqlite file, deleted the pycachefolders inside each of the apps, deleted all files inside the migrations folder for each app , except the init.py file, and then ran python manage.py makemigrations
and python manage.py migrate
. Also note that because you deleted the database you will have to create a new superuser using python manage.py createsuperuser
. Hope this helps
我有同样的问题,使用 Django 1.10,这是我所做的,我删除了数据库 sqlite 文件,删除了每个应用程序内的pycache文件夹,删除了每个应用程序迁移文件夹内的所有文件,除了init.py 文件,然后跑python manage.py makemigrations
和python manage.py migrate
。另请注意,由于您删除了数据库,因此您必须使用python manage.py createsuperuser
. 希望这可以帮助
回答by winux
Do not delete your database file!
不要删除您的数据库文件!
Its correct to delete migration files and then run flush but deleting sqlite database file is wrong. This worked to me every time. If you are using other database, it will save you a lot of work and preparations.
删除迁移文件然后运行flush是正确的,但删除sqlite数据库文件是错误的。这每次都对我有用。如果您使用的是其他数据库,它将为您节省大量的工作和准备工作。
- delete all ".py" and ".pyc" files manually
- python manage.py flush
type "yes" to confirm - python manage.py makemigrations
- python manage.py migrate
- 手动删除所有“.py”和“.pyc”文件
- python manage.py 刷新
类型“是”以确认 - python manage.py makemigrations
- python manage.py 迁移
回答by WebComer
For me, just
对我来说,只要
python manage.py flush
deleted old db contents, so i was able to create records anew in Django 2.1.4.
删除了旧的数据库内容,所以我能够在 Django 2.1.4 中重新创建记录。
Don't forget to create new superuser:
不要忘记创建新的超级用户:
python manage.py createsuperuser