我在不同的架构上有两个表
CREATE TABLE IF NOT EXISTS db1.`invoice` ( `invoice_id` int(11) NOT NULL AUTO_INCREMENT, `qsales_sale_id` int(11) DEFAULT NULL, `invoice_id_from_dosage_id` int(11) NOT NULL, `number` int(11) DEFAULT NULL, `enterprise_name` varchar(100) DEFAULT NULL, `subsidiary_name` varchar(100) DEFAULT NULL, `subsidiary_address` varchar(200) DEFAULT NULL, `subsidiary_phone` varchar(40) DEFAULT NULL, `client_name` varchar(200) DEFAULT NULL, `nit` bigint(20) DEFAULT NULL, `was_paid` tinyint(1) DEFAULT NULL, PRIMARY KEY (`invoice_id`), KEY `fk_invoice_qsales_sale1_idx` (`qsales_sale_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=457 ;
CREATE TABLE IF NOT EXISTS db2.qsales_sale ( `qsales_sale_id` int(11) NOT NULL AUTO_INCREMENT, `qsales_order_type_id` int(11) NOT NULL, `client_id` int(11) NOT NULL, `total_cost` decimal(10,2) DEFAULT NULL, `currency` varchar(45) DEFAULT NULL, `created_at` datetime DEFAULT NULL, `has_discount` tinyint(1) DEFAULT NULL, `created_by` int(11) NOT NULL, `is_wholesaler` tinyint(1) NOT NULL DEFAULT '0', `payment_type` varchar(65) DEFAULT NULL, PRIMARY KEY (`qsales_sale_id`), KEY `fk_qsales_sale_qsales_order_type1_idx` (`qsales_order_type_id`), KEY `fk_qsales_sale_client1_idx` (`client_id`), KEY `fk_qsales_sale_employee1_idx` (`created_by`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=51 ;
然后我想添加一个外键 我尝试: 1。
alter table bd1.invoice
add foreign key fk_invoice_qsales_sale1(qsales_sale_id)
references db2.qsales_sale (qsales_sale_id)
on delete cascade
on update cascade;
ALTER TABLE db1.invoice
添加约束fk_invoice_qsales_sale1
外键(qsales_sale_id
)
参考db2
。qsales_sale
(qsales_sale_id
)在更新时不执行任何操作;
但我有这个错误:
Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (`qfix`.`#sql-a28_5`, CONSTRAINT `#sql-a28_5_ibfk_1` FOREIGN KEY (`qsales_sale_id`) REFERENCES `qsales`.`qsales_sale` (`qsales_sale_id`) ON DELETE CASCADE ON UPDATE CASCADE)
0.625 sec