在执行filter_type(self)方法时,报如下错误
来源:5-18 (资产探测、扫描发现进阶)章节知识回顾
慕桂英8092138
2018-06-29
在执行filter_type(self)方法时,报外键错误,具体报错信息如下:
django.db.utils.IntegrityError: (1452, 'Cannot add or update a child row: a foreign key constraint fails (`mydevops`.`virtualserverinfo`, CONSTRAINT `virtualserverinfo_vir_phy_id_45b1088b_fk_physicalserverinfo_id` FOREIGN KEY (`vir_phy_id`) REFERENCES `physicalserverinfo` (`id`))')
有一个地方想不清楚,在运行这个方法时,VirtualServerInfo表是空的,physicalserverinfo表也是空的,这样通过VirtualServerInfo表中的vir_phy_id字段,去关联physicalserverinfo表中的id,这肯定是有问题吧,因为后面那张表是空的。但参数老师的代码就是这样执行的
3回答
-
Jeson
2018-07-02
elif re.search("VMware",item_val[-1]):
mac_addr = self.return_key_mac(item_val[-3])
VirtualServerInfo.objects.create(server_ip=item_val[0],server_type=item_val[-1],system_ver=item_val[6],sys_hostname=item_val[7],mac=item_val[-3],sn=item_val[-2],vir_phy_id=1,conn_vir_id=int(self.key_sn_relate[sn_key]))
扫描到Vmware的时候,执行的是这一段。vir_phy_id=1,所以 PhysicalServerInfo是空的是允许的。
00 -
Jeson
2018-06-30
def filter_type(self): VirtualServerInfo.objects.all().delete() NetWorkInfo.objects.all().delete() PhysicalServerInfo.objects.all().delete() for item,item_val in self.key_dic_map.items(): mac_addr = self.return_key_mac(item_val[-3]) sn_key = self.return_true_string(item_val[-2]) if re.search("KVM",item_val[-1]) or re.search("QEMU",item_val[-1]): VirtualServerInfo.objects.create(server_ip=item_val[0],server_type=item_val[-1],system_ver=item_val[6],sys_hostname=item_val[7],mac=item_val[-3],sn=item_val[-2],vir_phy_id=1,conn_vir_id=int(self.key_sn_relate[mac_addr])) elif re.search("VMware",item_val[-1]): mac_addr = self.return_key_mac(item_val[-3]) VirtualServerInfo.objects.create(server_ip=item_val[0],server_type=item_val[-1],system_ver=item_val[6],sys_hostname=item_val[7],mac=item_val[-3],sn=item_val[-2],vir_phy_id=1,conn_vir_id=int(self.key_sn_relate[sn_key])) elif re.search("Network",item_val[-1]): NetWorkInfo.objects.create(host_ip=item_val[0],host_name=item_val[7],net_cab_id=1,sn=item_val[-2]) # print "Network...........",item_val[-1] else: self.key_ip_relate[item] = item_val[0].split(',')[0] if sn_key and sn_key != 'NotSpecified': PhysicalServerInfo.objects.create(server_ip=item_val[0],machine_brand=item_val[-1],system_ver=item_val[6],sys_hostname=item_val[7],mac=item_val[-3],sn=item_val[-2],ser_cabin_id=1,conn_phy_id=int(self.key_sn_relate[sn_key])) else: PhysicalServerInfo.objects.create(server_ip=item_val[0],machine_brand=item_val[-1],system_ver=item_val[6],sys_hostname=item_val[7],mac=item_val[-3],sn=item_val[-2],ser_cabin_id=1,conn_phy_id=int(self.key_sn_relate[mac_addr])) # print "Machine............",item_val[-1] return True
当然执行了,你看PhysicalServerInfo入库这段的逻辑,是非"KVM"、"WMware"、"Network"类型的设备才会执行入库PhysicalServerInfo。key_dic_map.items(),你先把这段内容打印下。调试下原因,分析下
022018-07-01 -
Jeson
2018-06-29
是的,所以先需要入物理设备这张表数据physicalserverinfo,通常是这张表对应扫描到的物理机数据没有入进去,导致外键关联才出现的问题。
建议你debug下,这个执行过程,打印一些信息出来追下为什么physicalserverinfo表的数据没有入进去
022018-06-30
相似问题