Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8770053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:28:49+00:00 2026-06-13T17:28:49+00:00

I searched the StackOverflow, but I couldn’t find an answer to my specific question.

  • 0

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 ;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T17:28:50+00:00Added an answer on June 13, 2026 at 5:28 pm

    I found the answer to my own problem and thought it might help others in a similar situation.

    The problem is that trying to update using an insert, you have to think in terms of all the constraints of inserting. Any values that are undefined have to have defaults. I had a default for ‘carrinho_id’, but it was for a record that was already deleted, and for that reason I got the error.

    It turned out that that wasn’t my only problem. In order for MySql to detect a duplicate, you have to specify a row that can only be unique. Since I left out ‘id’ it simply added a new row, subracting the value for quantity from zero. It’s also important to note that you can subtract quantities in that way only if the column is unsigned.

    In the end, I succeeded by specifying both the primary key and the foreign key:

    INSERT INTO itens (id, carrinho_id, prod_id, qtd) VALUES (225, 75, 410, 1),(226, 75, 442, -1)
    ON DUPLICATE KEY UPDATE qtd = qtd + VALUES(qtd)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I searched StackOverflow but couldn't find the answer to this question. Suppose I have
I've searched all forums recently including StackOverflow, but couldn't find any solution how to
I have already googled and searched stackoverflow but I can find nothing related to
I searched StackOverflow before posting this question but I wasn't able to find the
I have searched stackoverflow for a question like mine but most of the questions
I searched in google and stackoverflow for my problem, but couldn't find a good
I have searched the entire forum but couldn't find what i was looking for.
I searched the Eclipse settings, the web and StackOverflow, but I couldn't find an
I have searched all over the Google and StackOverFlow, but still did not find
I searched the net and also here at stackoverflow, but I couldn't find any

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.