【发布时间】:2012-06-20 20:04:03
【问题描述】:
我正在跟进question that I asked earlier,在该question that I asked earlier 中,我试图寻求从愚蠢/写得不好的mysql 查询到postgresql 的转换。我相信我成功了。无论如何,我使用的是手动从 mysql 数据库移动到 postgres 数据库的数据。我正在使用如下所示的查询:
UPDATE krypdos_coderound cru
set is_correct = case
when t.kv_values1 = t.kv_values2 then True
else False
end
from
(select cr.id,
array_agg(
case when kv1.code_round_id = cr.id
then kv1.option_id
else null end
) as kv_values1,
array_agg(
case when kv2.code_round_id = cr_m.id
then kv2.option_id
else null end
) as kv_values2
from krypdos_coderound cr
join krypdos_value kv1 on kv1.code_round_id = cr.id
join krypdos_coderound cr_m
on cr_m.object_id=cr.object_id
and cr_m.content_type_id =cr.content_type_id
join krypdos_value kv2 on kv2.code_round_id = cr_m.id
WHERE
cr.is_master= False
AND cr_m.is_master= True
AND cr.object_id=%s
AND cr.content_type_id=%s
GROUP BY cr.id
) t
where t.id = cru.id
""" % ( self.object_id, self.content_type.id)
)
我有理由相信这很有效。然而,这又引出了一个新问题。尝试提交时,我从 django 收到一条错误消息:
IntegrityError at (some url):
duplicate key value violates unique constraint "krypdos_value_pkey"
我查看了此处发布的一些回复,但还没有找到解决问题的方法(尽管相关问题已经引起了一些有趣的阅读)。我在我的日志中看到了这一点,这很有趣,因为我从不明确调用 insert-django 必须处理它:
STATEMENT: INSERT INTO "krypdos_value" ("code_round_id", "variable_id", "option_id", "confidence", "freetext")
VALUES (1105935, 11, 55, NULL, E'')
RETURNING "krypdos_value"."id"
但是,尝试运行它会导致重复键错误。实际错误在下面的代码中抛出。
# Delete current coding CodeRound.objects.filter(object_id=o.id,content_type=object_type,is_master=True).delete()
code_round = CodeRound(object_id=o.id,content_type=object_type,coded_by=request.user,comments=request.POST.get('_comments',None),is_master=True)
code_round.save()
for key in request.POST.keys():
if key[0] != '_' or key != 'csrfmiddlewaretoken':
options = request.POST.getlist(key)
for option in options:
Value(code_round=code_round,variable_id=key,option_id=option,confidence=request.POST.get('_confidence_'+key, None)).save() #This is where it dies
# Resave to set is_correct
code_round.save()
o.status = '3'
o.save(
我已经检查了序列等,它们似乎是有序的。在这一点上,我不确定该怎么做——我认为这是 django 的结果,但我不确定。任何反馈将不胜感激!
【问题讨论】:
-
旁白:根据德摩根定律,您的条件
key[0] != '_' or key != 'csrfmiddlewaretoken'等同于not (key[0] == '_' and key == 'csrfmiddlewaretoken')。应该很容易看出,内部条件永远不满足,所以它相当于not (False),或者换句话说True。但是,为什么还要打扰if? -
python manage.py sqlsequencereset <app> | python manage.py dbshell -
这个先前的答案提供了更多的细节和主题:stackoverflow.com/questions/244243/…
标签: django postgresql duplicates unique-constraint database-integrity