如何解决不存在的字段的完整性错误?

时间:2014-07-05 20:16:31

标签: django postgresql django-south

我正在使用django 1.6,South和POstgres。 我是南方的新手,我觉得我的数据库表与模式或以前的任何迁移都不同。 特别是,我认为评论中的提问者存在一个字段(我改变了这个设计)。如何让南方正确更新。 我已经尝试过schemamigration --auto和migrate,它表示不会进行任何更改。

views.py:

class AnswerCreate(CreateView):
model = Comment
template_name = 'answer_threads/create_thread.html'
fields = ['is_anon', 'comment_body']
success_url = '/conversations/'

def form_valid(self, form):
    question_pk = self.request.path.split('/')[-2]
    question = Question.objects.get(pk=question_pk)
    questioner = question.questioner
    commenter = self.request.user
    self.object = form.save(commit=False)
    self.object.question = question
    self.object.commenter = commenter
    new_comment = self.object.save()
    try:
        #thread already exists 
        thread = Thread.objects.get(question=question, 
                                    commenter=commenter)
        thread.last_comment = new_comment
        thread.save()
    except Thread.DoesNotExist:
        thread = Thread.objects.create(question=question, 
                                       questioner=questioner, 
                                       commenter=commenter, 
                                       last_comment=new_comment)
    return super(AnswerCreate, self).form_valid(form)

也是inpectdb:

class AnswerThreadsComment(models.Model):
id = models.IntegerField(primary_key=True)
comment_body = models.TextField()
commenter = models.ForeignKey('AuthUser')
questioner = models.ForeignKey('AuthUser')
question = models.ForeignKey('QuestionsQuestion')
order = models.IntegerField()
is_anon = models.BooleanField()
when_asked = models.DateTimeField()
thread = models.BigIntegerField()
class Meta:
    managed = False
    db_table = 'answer_threads_comment'

models.py

class Comment(models.Model):
comment_body = models.TextField()
is_anon = models.BooleanField(default=True)
when_asked = models.DateTimeField(auto_now_add=True)
question = models.ForeignKey(Question)
commenter = models.ForeignKey(User)

class Thread(models.Model):
question = models.ForeignKey(Question)
last_comment = models.ForeignKey(Comment)
commenter = models.ForeignKey(User, related_name='Comment.commenter')
questioner = models.ForeignKey(User, related_name='Question.questioner')

错误消息:

IntegrityError at /questions/create_answer/1/ 
null value in column "questioner_id" violates not-null constraint
DETAIL:  Failing row contains (8, 3.59, 2, null, 1, null, 
                               f, 2014-07-05 19:59:14.639631+00, 
                               null).
Request Method: POST 
Request URL:    **redacted**/questions/create_answer/1/
Django Version: 1.6.5
Exception Type: IntegrityError
Exception Value:    
null value in column "questioner_id" violates not-null constraint
DETAIL:  Failing row contains (8, 3.59, 2, null, 1, null, 
                               f, 2014-07-05 19:59:14.639631+00, 
                               null).
Exception Location:  in execute, line 53    
Python Version: 2.7.6

更远的错误:

new_comment = self.object.save()
Variable    Value
questioner  <User: 3.46>
form    <django.forms.models.CommentForm object at 0x7f63694546d0>
self    <answer_threads.views.AnswerCreate object at 0x7f6369454690>
commenter   <SimpleLazyObject: <User: 3.46>>
question    <Question: 3.33>
question_pk u'1'

1 个答案:

答案 0 :(得分:0)

所以其他人:

清除:

./manage.py migrate answer_threads zero

然后只是

./manage.py schemamigration answer_threads --auto

然后

./manage.py migrate answer_threads