【问题标题】:Modify Tables in database修改数据库中的表
【发布时间】:2012-08-21 09:41:15
【问题描述】:

我有这个模型

class Category(models.Model):
category_name= models.CharField(max_length=255)
sub_category_ID = models.IntegerField(null=True, blank=True)
def __unicode__(self):
    return self.category_name

我的表中已有数据,但我想将sub_category_ID 更改为不删除整个数据库。

class Category(models.Model):
category_name= models.CharField(max_length=255)
sub_category_ID = models.ForeignKey('self',null=True, blank=True)
def __unicode__(self):
    return self.category_name

所以我在更改模型后运行了 syncdb,它给了我警告。

The following content types are stale and need to be deleted:
uTriga | event_event_category
Any objects related to these content types by a foreign key will also
be deleted. Are you sure you want to delete these content types?
If you're unsure, answer 'no'.`

我输入了yes,现在出现错误

column app_category.sub_category_ID_id does not exist

列 uTriga_category.sub_category_ID_id 不存在

【问题讨论】:

标签: django django-models


【解决方案1】:

默认情况下,Django syncdb 机制不允许模型更改。必须删除并重新创建整个数据库。只能在两次 syncb 运行之间添加新模型。

因此,您可能想要使用一些“迁移”工具来允许数据库模式的渐进演进。使用一个恕我直言,这几乎总是一个非常好的主意。

South 是最著名的 Django,到目前为止我对它非常满意。

【讨论】:

  • 我刚刚安装了 South-0.7.6。你能告诉我如何根据我的问题中的问题使用它吗?
  • @YehonathanQuartey - 您应该阅读南页上的文档,然后自己尝试一下。如果您在使用 south 时遇到问题,请发布一个新的详细问题。
  • 很高兴为您提供帮助,欢迎来到 Stack Overflow。如果此答案或任何其他答案解决了您的问题,请将其标记为已接受。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-15
相关资源
最近更新 更多