简体   繁体   English

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

[英]How to reset db in Django? I get a command 'reset' not found error

Following this Django by Example tutotrial here: http://lightbird.net/dbe/todo_list.html在此 Django 之后通过示例教程在这里: 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: “这会改变我们的表格布局,我们必须要求 Django 重置和重新创建表格:

manage.py reset todo; manage.py syncdb manage.py reset todo; manage.py syncdb " manage.py reset todo; manage.py syncdb

though, when I run manage.py reset todo , I get the error:但是,当我运行manage.py reset todo时,出现错误:

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

Is this because I am using sqlite3 and not postgresql?这是因为我使用的是 sqlite3 而不是 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返回错误:

$ 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:所以我在 settings.py 中将“todo”添加到我的 INSTALLED_APPS 中,然后再次运行python manage.py sqlclear todo ,导致此错误:

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

reset已被 Django 1.5 替换为flush ,请参阅:

python manage.py help flush

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.我发现删除所有表的两种方法,都需要核心 django 以外的东西。

If you're on Heroku, drop all the tables with pg:reset:如果您使用 Heroku,请使用 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:如果你可以安装 Django 扩展,它有一种方法可以完全重置:

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

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.与 LisaD 的回答类似, Django Extensions有一个很棒的 reset_db 命令,可以完全删除所有内容,而不仅仅是像“flush”那样截断表。

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.做一个 reset_db 解决了这个问题。

if you are using Django 2.0 Then如果您使用的是 Django 2.0 那么

python manage.py flush 

will work将工作

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

With django 1.11, simply delete all migration files from the migrations folder of each application (all files except __init__.py ).使用 django 1.11,只需从每个应用程序的migrations文件夹中删除所有迁移文件(除__init__.py之外的所有文件)。 Then然后

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

And voilla, your database has been completely reset.瞧,您的数据库已完全重置。

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

Just a follow up to @LisaD's answer.只是对@LisaD 的回答的跟进。
As of 2016 ( Django 1.9 ), you need to type:截至 2016 年( Django 1.9 ),您需要输入:

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.这将为您在 Heroku 中提供一个全新的数据库。

python manage.py flush

deleted old db contents,删除旧的数据库内容,

Don't forget to create new superuser:不要忘记创建新的超级用户:

python manage.py createsuperuser
  1. Just manually delete you database.只需手动删除您的数据库。 Ensure you create backup first (in my case db.sqlite3 is my database)确保首先创建备份(在我的情况下db.sqlite3是我的数据库)

  2. Run this command manage.py migrate运行此命令manage.py migrate

I use python manage.py flush on django 4.1 with postreSQL我在 django 4.1 上使用python manage.py flush和 postreSQL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM