【发布时间】:2010-12-14 17:53:48
【问题描述】:
我正在尝试在 Django 中重置数据库,使用:
python manage.py reset app
但得到以下错误:
Error: Error: app couldn't be reset. Possible reasons:
* The database isn't running or isn't configured correctly.
* At least one of the database tables doesn't exist.
* The SQL was invalid.
Hint: Look at the output of 'django-admin.py sqlreset app'. That's the SQL this command wasn't able to run.
The full error: cannot drop table app_record because other objects depend on it
HINT: Use DROP ... CASCADE to drop the dependent objects too.
这就是我的 models.py 文件的样子:
class Record(models.Model):
name = models.CharField(max_length=50, db_index=True)
year = models.IntegerField(blank=True, null=True)
def __unicode__(self):
return self.name
class Class(models.Model):
record = models.ForeignKey(Record)
def __unicode__(self):
return self.id
我知道我需要在删除和重新创建数据库的 SQL 中使用 DROP... CASCADE 命令(django-admin.py 的输出)。
但是如何直接从 models.py 中编辑该 SQL?
更新
好的,我想出了如何手动删除表(数据库是 postgres),在这里为有同样问题的人记录了它:
python manage.py dbshell
# drop table app_record cascade;
# \q
python manage.py reset app
仍然想知道是否有人有更好的方法来做到这一点:)
【问题讨论】:
-
你输入什么命令来得到它?
python manage.py reset app?或者您是否尝试重置特定表? -
对不起 - 是的,“python manage.py reset app”。
-
一个简单的解决方案就是手动使用 DROP... CASCADE 命令,然后重新创建数据库... 谁能给我一个例子来说明如何做到这一点?
-
好的,我想出了如何手动操作(数据库是 postgres),在这里为任何有同样问题的人指出:python manage.py dbshell # drop table app_record cascade; # \q python manage.py reset app 仍然想知道是否有人有更好的方法,不过:)