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 question.(5)

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

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/How to avoid the error "cannot delete or update a parent row: a foreign key constraint fails"?
Next
Answered
Léa Hamel
  • 7
Léa Hamel
Asked: May 17, 20222022-05-17T12:40:05+00:00 2022-05-17T12:40:05+00:00In: Programs

How to avoid the error “cannot delete or update a parent row: a foreign key constraint fails”?

  • 7

. Advertisement .

..3..

. Advertisement .

..4..

How to solve the problem – cannot delete or update a parent row: a foreign key constraint fails? I have the sample detail:

DELETE FROM `jobs` WHERE `job_id` =1 LIMIT 1
CREATE TABLE IF NOT EXISTS `advertisers` (
  `advertiser_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `password` char(32) NOT NULL,
  `email` varchar(128) NOT NULL,
  `address` varchar(255) NOT NULL,
  `phone` varchar(255) NOT NULL,
  `fax` varchar(255) NOT NULL,
  `session_token` char(30) NOT NULL,
  PRIMARY KEY (`advertiser_id`),
  UNIQUE KEY `email` (`email`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;
 
 
 INSERT INTO `advertisers` (`advertiser_id`, `name`, `password`, `email`, `address`, `phone`, `fax`, `session_token`) VALUES
 (1, 'TEST COMPANY', '', '', '', '', '', '');
 
 CREATE TABLE IF NOT EXISTS `jobs` (
  `job_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `advertiser_id` int(11) unsigned NOT NULL,
  `name` varchar(255) NOT NULL,
  `shortdesc` varchar(255) NOT NULL,
  `longdesc` text NOT NULL,
  `address` varchar(255) NOT NULL,
  `time_added` int(11) NOT NULL,
  `active` tinyint(1) NOT NULL,
  `moderated` tinyint(1) NOT NULL,
  PRIMARY KEY (`job_id`),
  KEY `advertiser_id` (`advertiser_id`,`active`,`moderated`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;
 
 
 INSERT INTO `jobs` (`job_id`, `advertiser_id`, `name`, `shortdesc`, `longdesc`, `address`, `active`, `moderated`) VALUES
 (1, 1, 'TEST', 'TESTTEST', 'TESTTESTES', '', 0, 0);
 
 ALTER TABLE `advertisers`
  ADD CONSTRAINT `advertisers_ibfk_1` FOREIGN KEY (`advertiser_id`) REFERENCES `jobs` (`advertiser_id`);

While I was running it, I found the warning message:

#1451 - Cannot delete or update a parent row: a foreign key constraint fails 
 (paymesomething.advertisers, CONSTRAINT advertisers_ibfk_1 FOREIGN KEY 
 (advertiser_id) REFERENCES jobs (advertiser_id))

That is my question in my midterm exam, and it is urgent. I searched the solutions on some websites, but I didn’t get it. I may miss any line or other changes. I appreciate your assistance!

a foreign key constraint fails
  • 2 2 Answers
  • 248 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    hdtutoria Expert
    2022-06-04T02:59:06+00:00Added an answer on June 4, 2022 at 2:59 am

    The cause: I think the cause is that you must delete the advertisers table row before you can delete the jobs table row that it refers to. This:

    ALTER TABLE `advertisers`
      ADD CONSTRAINT `advertisers_ibfk_1` FOREIGN KEY (`advertiser_id`)
          REFERENCES `jobs` (`advertiser_id`);

    is the exact opposite of what should be done. As is, you’d have to have a record in the jobs table before the advertisers.

    The solution: You need to do as the following:

    ALTER TABLE `jobs`
      ADD CONSTRAINT `advertisers_ibfk_1` FOREIGN KEY (`advertiser_id`)
          REFERENCES `advertisers` (`advertiser_id`);

    Your delete statement will operate after you fix the foreign key relationship.

    • 11
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Ethan Mercier
    2022-05-25T19:56:05+00:00Added an answer on May 25, 2022 at 7:56 pm

    It is simple to disable foreign key checks and then make the necessary changes.

    SET FOREIGN_KEY_CHECKS=0; -- to disable them
    SET FOREIGN_KEY_CHECKS=1; -- to re-enable them
    • 19
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

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.