我有一个从模型对象继承的模型ThreadedComment. ThreadedComment自身没有唯一的主键,它依赖于Object的主键(“ ID”).这是模型的构造方式:
class Object(models.Model):
permalink = models.CharField(max_length=128)
status = models.IntegerField()
version = models.IntegerField()
class ThreadedComment(Object):
parent = models.ForeignKey('self', null=True, blank=True, default=None, related_name='children')
parent_object = models.OnetoOneField(Object, parent_link=True)
# other fields follow
在django 1.2.3之前,这一直很好用,但是当我升级到django 1.2.5(1.3有相同的问题)时,在尝试运行任何测试时就会发生这种情况:
Error: Database test_db Couldn't be flushed. Possible reasons:
* The database isn't running or isn't configured correctly.
* At least one of the expected database tables doesn't exist.
* The sql was invalid.
Hint: Look at the output of 'django-admin.py sqlflush'. That's the sql this command wasn't able to run.
The full error: relation "threadedcomments_threadedcomment_id_seq" does not exist
LINE 1: SELECT setval('"threadedcomments_threadedcomment_id_seq"', 1...
问题在于生成和执行sql文件的“ sqlflush”命令.有问题的行是:
SELECT setval(pg_get_serial_sequence('"threadedcomments_threadedcomment"','id'), 1, false);
这显然失败了,因为该表中没有“ id”列.
有趣的是,Django 1.2.3版本产生了类似的输出:
SELECT setval('"threadedcomments_threadedcomment_id_seq"', 1, false);
但无论如何,测试仍在进行,所以我之前没有注意到这个问题.
我在这里做错了什么?模型定义是否不正确,即即使我不需要与对象一对一的关系,我也需要在线程注释中有一个主键吗?从1.0到1.1到1.2.3一直到现在以1.2.5中断,它如何一直正常工作?
解决方法:
原来这是一个django错误.此处详细信息:http://code.djangoproject.com/ticket/12728
这是我们的临时解决方法:https://bitbucket.org/filmaster/filmaster-test/changeset/afbac905cf63