Django:flush命令不会完全清除数据库,重置失败 [英] Django: flush command doesnt completely clear database, reset fails

查看:222
本文介绍了Django:flush命令不会完全清除数据库,重置失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我重写了很多我的模型,由于我刚刚运行一个测试服务器,我做了./manage.py重置myapp来重置db表,一切都正常工作。



但是我这次尝试这样做,我收到一个错误,



完整的错误:所以我认为我只是打扰整个网站,开始新鲜,所以我做了./manage.py flush(),/ etc /然后做了一个syncdb这没有引起错误并删除我的所有数据,但是它没有更新数据库,因为当我尝试访问任何my_app的对象,我得到一个列没有找到错误我认为flush应该是删除所有表,syncdb表示没有添加任何设备。



我认为错误与我将标记器模型更改为具有名称所有者的外键绑定到另一个对象。



我已经尝试将foreign_name添加到外键参数,没有什么似乎正常。

解决方案


我以为冲洗应该会丢下所有的表。


< blockquote>

不。根据文档 manage.py flush 不会丢弃表。相反,它执行以下操作:


将数据库返回到执行syncdb之后的状态。这意味着所有数据将从数据库中删除,任何后同步处理程序将被重新执行,并且将重新安装initial_data fixture。


Django Book第10章中所述,在进行更改到数据库模式部分,


syncdb 只创建不会但存在于您的数据库中 - 它不会同步模型中的更改或执行模型删除。如果您添加或更改模型的字段,或者删除模型,则需要手动对数据库进行更改。


因此,要解决您的问题,您将需要:


  1. 删除数据库并重新发行 manage.py执行syncdb 。这是当我仍在开发数据库模式时使用的过程。我使用一个initial_data夹具来安装一些测试数据,当数据库模式发生变化时,它也需要更新。

  2. 手动发出SQL命令来修改数据库模式。

  3. 使用 South


I rewrote a lot of my models, and since I am just running a test server, I do ./manage.py reset myapp to reset the db tables and everything has been working fine.

But I tried to do it this time, and I get an error,

"The full error: contraint owner_id_refs_id_9036cedd" of relation "myapp_tagger" does not exist"

So I figured I would just nuke the whole site and start fresh. So i did ./manage.py flush then did a syncdb this did not raise an error and deleted all my data, however it did not update the database since when I try to access any of my_app's objects, i get a column not found error. I thought that flush was supposed to drop all tables. The syncdb said that no fixtures were added.

I assume the error is related to the fact that I changed the tagger model to having a foreignkey with a name owner tied to another object.

I have tried adding related_name to the foreignkey arguments and nothing seems to be working.

解决方案

I thought that flush was supposed to drop all tables.

No. According to the documentation, manage.py flush doesn't drop the tables. Instead it does the following:

Returns the database to the state it was in immediately after syncdb was executed. This means that all data will be removed from the database, any post-synchronization handlers will be re-executed, and the initial_data fixture will be re-installed.

As stated in chapter 10 of The Django Book in the "Making Changes to a Database Schema" section,

syncdb merely creates tables that don't yet exist in your database — it does not sync changes in models or perform deletions of models. If you add or change a model's field, or if you delete a model, you’ll need to make the change in your database manually.

Therefore, to solve your problem you will need to either:

  1. Delete the database and reissue manage.py syncdb. This is the process that I use when I'm still developing the database schema. I use an initial_data fixture to install some test data, which also needs to be updated when the database schema changes.
  2. Manually issue the SQL commands to modify your database schema.
  3. Use South.

这篇关于Django:flush命令不会完全清除数据库,重置失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆