MySQL上的Odd IntegrityError:#1452 [英] Odd IntegrityError on MySQL: #1452

查看:144
本文介绍了MySQL上的Odd IntegrityError:#1452的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一种奇怪的事情,但我尽量尽可能地解释。我有两个模型:一个代表电子邮件(Message),另一个是销售主管(AffiliateLead)。当通过网站提交表单时,系统会产生潜在客户,然后发送电子邮件。消息模型有一个可选的FK回到铅。从消息模型文件:

  lead = models.ForeignKey('tracking.AffiliateLead',blank = True,null = True) 

现在,这个基本的shell工作:

 来自tracking.models import加盟,AffiliateLead 
from messages.models import消息
from django.contrib.auth.models import User

u = User.objects.get(username ='testguy')
a = Affiliate.objects.get(affiliate_id ='ACD023')
l = AffiliateLead(affiliate = a)
l.save()
m = Message(recipient = u,sender = u,subject ='s',body ='a',lead = l)
m.save()

然而,窗体视图本身没有。当我尝试保存指向AffiliateLead的消息时,它会抛出一个IntegrityError:

 (1452,'无法添加或更新子行:外键约束失败(`app`.`messages_message`,CONSTRAINT`lead_id_refs_id_6bc546751c1f96` FOREIGN KEY(`lead_id`)参考`tracking_affiliatelead`(`id`))')

尽管这个视图只是采取表单,创建和保存AffiliateLead,然后创建和(尝试)保存消息。实际上,当这个错误被抛出来的时候,我可以进入MySQL,看到新创建的引导。当我在保存前立即从数据库中重新获取数据,甚至会在视图中抛出此错误:

  af_lead = AffiliateLead。 object.get(id = af_lead.id)
msg.lead = af_lead
msg.save()

最后,如果我立即刷新(重新提交表单),它可以工作。没有IntegrityError。如果我有Django打印出它正在做的事情,我可以看到它是在尝试插入消息之前插入AffiliateLead,而Message INSERT正在使用正确的AffiliateLead ID。在这一点上我真的很沮丧。我甚至尝试过手动事务处理无效。

解决方案

我不知道为什么会发生,但是我做了似乎找到一个解决方案。我正在使用南方管理DB;它创建了作为InnoDB和AffiliateLead作为MyISAM的消息。将AffiliateLead表更改为InnoDB,结束了IntegrityErrors。希望这有助于别人。


This is sort of an odd one but I'll try to explain as best I can. I have 2 models: one representing an email message (Message), the other a sales lead (AffiliateLead). When a form is submitted through the site, the system generates a lead and then emails. The Message model has an optional FK back to the Lead. From the Message models file:

lead = models.ForeignKey('tracking.AffiliateLead', blank=True, null=True)

Now, this basic shell works:

from tracking.models import Affiliate, AffiliateLead
from messages.models import Message
from django.contrib.auth.models import User

u = User.objects.get(username='testguy')
a = Affiliate.objects.get(affiliate_id = 'ACD023')
l = AffiliateLead(affiliate = a)
l.save()
m = Message(recipient=u, sender=u, subject='s', body='a', lead=l)
m.save()

However, the form view itself does not. It throws an IntegrityError when I try to save a Message that points to an AffiliateLead:

(1452, 'Cannot add or update a child row: a foreign key constraint fails (`app`.`messages_message`, CONSTRAINT `lead_id_refs_id_6bc546751c1f96` FOREIGN KEY (`lead_id`) REFERENCES `tracking_affiliatelead` (`id`))')

This is despite the fact that the view is simply taking the form, creating and saving the AffiliateLead, then creating and (trying) to save the Message. In fact, when this error is thrown, I can go into MySQL and see the newly-created lead. It even throws this error in the view when I re-retrieve the lead from the DB immediately before saving:

af_lead = AffiliateLead.objects.get(id = af_lead.id)
msg.lead = af_lead
msg.save()

Finally, if I immediately refresh (re-submitting the form), it works. No IntegrityError. If I have Django print out the SQL it's doing, I can indeed see that it is INSERTing the AffiliateLead before it tries to INSERT the Message, and the Message INSERT is using the correct AffiliateLead ID. I'm really stumped at this point. I've even tried manual transaction handling to no avail.

解决方案

I'm not exactly sure why it happened, but I did seem to find a solution. I'm using South to manage the DB; it created Messages as InnoDB and AffiliateLead as MyISAM. Changing the AffiliateLead table to InnoDB ended the IntegrityErrors. Hope this helps someone else.

这篇关于MySQL上的Odd IntegrityError:#1452的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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