MySQL Workbench错误1452

时间:2016-02-17 08:30:45

标签: mysql mysql-error-1452

我尝试使用MySQL Workbench提供的工具尝试从表创建外键到另一个表,但我得到的只是这个错误:

select o.*,
       (coalesce(Refund_Amount, 0) + coalesce(sv, 0)) as SettledAmount,
       (Invoice_No_Amt - coalesce(Refund_Amount, 0) - coalesce(sv, 0)) as netAmount
from fk_orders o left join
     (select invoice_no, sum(Settlement_Value) as sv
      from fk_payments
      group by invoice_no
     ) p
     on o.invoice_no = p.invoice_no;

让我最困惑的是行ERROR 1452: Cannot add or update a child row: a foreign key constraint fails (`mediacom`.`#sql-758_4`, CONSTRAINT `med_agente_ibfk_1` FOREIGN KEY (`id_agenzia`) REFERENCES `med_agenzia` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION) SQL Statement: ALTER TABLE `mediacom`.`med_agente` ADD CONSTRAINT `med_agente_ibfk_1` FOREIGN KEY (`id_agenzia`) REFERENCES `mediacom`.`med_agenzia` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION mediacom,因为我使用的真实查询是:

#sql-758_4

索引插入正常但其余部分被忽略,为什么?

这是med_agente表

ALTER TABLE `mediacom`.`med_agente` 
ADD INDEX `med_agente_ibfk_1_idx` (`id_agenzia` ASC)  COMMENT '';
ALTER TABLE `mediacom`.`med_agente` 
ADD CONSTRAINT `med_agente_ibfk_1`
  FOREIGN KEY (`id_agenzia`)
  REFERENCES `mediacom`.`med_agenzia` (`id`)
  ON DELETE NO ACTION
  ON UPDATE NO ACTION;

这是med_agenzia

CREATE TABLE `med_agente` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `nome` varchar(225) DEFAULT NULL,
  `cognome` varchar(225) DEFAULT NULL,
  `disabilitato` tinyint(1) DEFAULT NULL,
  `mod_time` datetime DEFAULT NULL,
  `mod_user` varchar(255) DEFAULT NULL,
  `codmobile` decimal(13,0) DEFAULT NULL,
  `codfisso` decimal(13,0) DEFAULT NULL,
  `id_agenzia` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `med_agente_ibfk_1_idx` (`id_agenzia`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

问题在哪里???

1 个答案:

答案 0 :(得分:1)

运行查询之前

运行:

mAdapter.notifyItemChanged(position)

然后将其设置为1

 SET FOREIGN_KEY_CHECKS=0;
运行Alter查询后