如何解决不存在的字段的完整性错误? [英] How can I solve an integrity error for a field that doesn't exist?

查看:61
本文介绍了如何解决不存在的字段的完整性错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django 1.6,South和POstgres.我是新来的南方人,我感觉我的数据库表与架构或以前的迁移不同.特别是,我认为发问者在评论中存在一个领域(我改变了这种设计).我如何南下以正确更新.我尝试了schemamigration --auto和migration,它说没有更改.

I am using django 1.6, South, and POstgres. I'm new to south, and I feel like my db tables in are different from the schema or any previous migrations. In particular, I think a field exists for the questioner in comments (I changed away from this design). How do I get south to properly update. I've tried schemamigration --auto and migrate and it says no changes to be made.

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

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'

推荐答案

对于其他人:

要清除:

./manage.py migrate answer_threads zero

然后就

./manage.py schemamigration answer_threads --auto

然后

./manage.py migrate answer_threads

这篇关于如何解决不存在的字段的完整性错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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