如何在Django中重置数据库?我收到命令“重置”未找到错误

问题:如何在Django中重置数据库?我收到命令“重置”未找到错误

在此处按照示例通过Django进行学习:http://lightbird.net/dbe/todo_list.html

该教程说:

“这改变了我们的表布局,我们必须要求Django重置并重新创建表:

manage.py reset todo; manage.py syncdb

但是,当我运行时manage.py reset todo,出现错误:

$ python manage.py reset todo                                       
- Unknown command: 'reset'

这是因为我使用的是sqlite3而不是Postgresql吗?

有人可以告诉我重置数据库的命令是什么吗?

命令:python manage.py sqlclear todo返回错误:

$ python manage.py sqlclear todo    
CommandError: App with label todo could not be found.    
Are you sure your INSTALLED_APPS setting is correct?

因此,我在settings.py的INSTALLED_APPS中添加了“ todo”,然后python manage.py sqlclear todo再次运行,导致此错误:

$ python manage.py sqlclear todo                                      
- NameError: name 'admin' is not defined

Following this Django by Example tutotrial here: http://lightbird.net/dbe/todo_list.html

The tutorial says:

“This changes our table layout and we’ll have to ask Django to reset and recreate tables:

manage.py reset todo; manage.py syncdb

though, when I run manage.py reset todo, I get the error:

$ python manage.py reset todo                                       
- Unknown command: 'reset'

Is this because I am using sqlite3 and not postgresql?

Can somebody tell me what the command is to reset the database?

The command: python manage.py sqlclear todo returns the error:

$ python manage.py sqlclear todo    
CommandError: App with label todo could not be found.    
Are you sure your INSTALLED_APPS setting is correct?

So I added ‘todo’ to my INSTALLED_APPS in settings.py, and ran python manage.py sqlclear todo again, resulting in this error:

$ python manage.py sqlclear todo                                      
- NameError: name 'admin' is not defined

回答 0

reset已被flushDjango 1.5 取代,请参阅:

python manage.py help flush

reset has been replaced by flush with Django 1.5, see:

python manage.py help flush

回答 1

看起来“冲洗”答案适用于部分情况,但不是全部情况。我不仅需要刷新数据库中的值,还需要正确地重新创建表。我还没有使用迁移(早期),所以我确实需要删除所有表。

我发现删除所有表的两种方法都需要核心django以外的东西。

如果您使用的是Heroku,请使用pg:reset删除所有表:

heroku pg:reset DATABASE_URL
heroku run python manage.py syncdb

如果您可以安装Django扩展,则可以通过以下方式完全重置:

python ./manage.py reset_db --router=default

It looks like the ‘flush’ answer will work for some, but not all cases. I needed not just to flush the values in the database, but to recreate the tables properly. I’m not using migrations yet (early days) so I really needed to drop all the tables.

Two ways I’ve found to drop all tables, both require something other than core django.

If you’re on Heroku, drop all the tables with pg:reset:

heroku pg:reset DATABASE_URL
heroku run python manage.py syncdb

If you can install Django Extensions, it has a way to do a complete reset:

python ./manage.py reset_db --router=default

回答 2

与LisaD的答案类似,Django扩展具有出色的reset_db命令,该命令可以完全删除所有内容,而不仅仅是像“ flush”一样截断表。

python ./manage.py reset_db

仅仅刷新表并不能解决在删除对象时发生的永久性错误。进行reset_db解决了该问题。

Similar to LisaD’s answer, Django Extensions has a great reset_db command that totally drops everything, instead of just truncating the tables like “flush” does.

python ./manage.py reset_db

Merely flushing the tables wasn’t fixing a persistent error that occurred when I was deleting objects. Doing a reset_db fixed the problem.


回答 3

如果您使用的是Django 2.0,那么

python manage.py flush 

将工作

if you are using Django 2.0 Then

python manage.py flush 

will work


回答 4

如果要清理整个数据库,则可以使用: python manage.py flush 如果要清理Django应用程序的数据库表,则可以使用: python manage.py migration appname 0

If you want to clean the whole database, you can use: python manage.py flush If you want to clean database table of a Django app, you can use: python manage.py migrate appname zero


回答 5

使用django 1.11,只需从migrations每个应用程序的文件夹中删除所有迁移文件(除外的所有文件__init__.py)。然后

  1. 手动删除数据库。
  2. 手动创建数据库。
  3. 运行python3 manage.py makemigrations
  4. 运行python3 manage.py migrate

瞧,您的数据库已完全重置。

With django 1.11, simply delete all migration files from the migrations folder of each application (all files except __init__.py). Then

  1. Manually drop database.
  2. Manually create database.
  3. Run python3 manage.py makemigrations.
  4. Run python3 manage.py migrate.

And voilla, your database has been completely reset.


回答 6

对我来说,这解决了问题。

heroku pg:reset DATABASE_URL

heroku run bash
>> Inside heroku bash
cd app_name && rm -rf migrations && cd ..
./manage.py makemigrations app_name
./manage.py migrate

For me this solved the problem.

heroku pg:reset DATABASE_URL

heroku run bash
>> Inside heroku bash
cd app_name && rm -rf migrations && cd ..
./manage.py makemigrations app_name
./manage.py migrate

回答 7

只是跟进@LisaD的答案。
截至2016年(Django 1.9),您需要输入:

heroku pg:reset DATABASE_URL
heroku run python manage.py makemigrations
heroku run python manage.py migrate

这将为您提供Heroku中的全新数据库。

Just a follow up to @LisaD’s answer.
As of 2016 (Django 1.9), you need to type:

heroku pg:reset DATABASE_URL
heroku run python manage.py makemigrations
heroku run python manage.py migrate

This will give you a fresh new database within Heroku.


回答 8

  1. 只需手动删除数据库即可。确保首先创建备份(在我的情况下db.sqlite3是我的数据库)

  2. 运行此命令 manage.py migrate

  1. Just manually delete you database. Ensure you create backup first (in my case db.sqlite3 is my database)

  2. Run this command manage.py migrate


回答 9

python manage.py flush

删除旧的数据库内容,

不要忘记创建新的超级用户:

python manage.py createsuperuser
python manage.py flush

deleted old db contents,

Don’t forget to create new superuser:

python manage.py createsuperuser