I searched the StackOverflow, but I couldn't find an answer to my specific question.
Recently I have become a big fan of updating records with a list, using "INSERT INTO...ON DUPLICATE KEY UPDATE." However, I had problems with the following query:
INSERT INTO itens (prod_id, qtd) VALUES (410, 1),(442, -1)
ON DUPLICATE KEY UPDATE qtd = qtd + VALUES(qtd)
That resulted in the error: "Cannot add or update a child row: a foreign key constraint fails."
Here is the structure of the table in question. "carrinho_id" is a foreign key. I was puzzled by the fact that I wasn't trying to do anything with the foreign key. I simply wanted to update the quantity.
CREATE TABLE IF NOT EXISTS `itens` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`carrinho_id` int(10) unsigned NOT NULL DEFAULT '1',
`prod_id` int(10) unsigned NOT NULL DEFAULT '1',
`qtd` int(12) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `carrinho_id` (`carrinho_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=229 ;