error code #1452 when adding foreign keys - mysql

Error
SQL query:
ALTER TABLE `bids` ADD FOREIGN KEY (`buyerID`) REFERENCES `e_trading_post`.`buyer`(`buyerID`)
ON DELETE CASCADE ON UPDATE CASCADE;
MySQL said:
1452 - Cannot add or update a child row: a foreign key constraint fails ('e_trading_post' .'#sql-15d48_6a8', CONSTRAINT `#sql-15d48_6a8_ibfk_1' FOREIGN KEY ('buyerID') REFERENCES 'buyer; ('buyerID') ON DELETE CASCADE ON UPDATE CASCADE)

This error means that this specific foreign key constraint cannot be created as it would be violated (is is violated right now).
The following query might help you to find the violating row:
select buyerID from bids where buyerID not in (select buyer from e_trading_post)

Related

ERROR 1452 - Cannot add or update a child row: a foreign key constraint fails

I need to make a forum using PHP and MySQL. When I try to add a question in my database PhpMyAdmin return me an ERROR 1452 - Cannot add or update a child row: a foreign key constraint fails.
I'm using this request to add a question :
INSERT INTO questions (titre_q, text_q, date_q) VALUES ("Help", "I'm Stuck", "2019-12-07");
#1452 - Cannot add or update a child row: a foreign key constraint fails (vogel_forum.questions, CONSTRAINT questions_ibfk_1 FOREIGN KEY (id) REFERENCES utilisateurs (id) ON DELETE CASCADE ON UPDATE CASCADE)
My database look like this :
Thank you for your help :)

ERROR for INSERT #1452 - Cannot add or update a child row: a foreign key constraint fails

I always get an error if I want to insert value in my tables..
SQL:
INSERT INTO `login`( `lo_password`, `lo_userName`, `lo_eMail`) VALUES ("sdsdf!D","Test!s1","test#test.com")
Error:
1452 - Cannot add or update a child row: a foreign key constraint fails (splitthebilldb.login, CONSTRAINT login_ibfk_1 FOREIGN KEY (lo_id) REFERENCES users (lo_id_login))
Your constraint is not in the right order :
CONSTRAINT login_ibfk_1 FOREIGN KEY (lo_id) REFERENCES users (lo_id_login))
should be :
CONSTRAINT login_ibfk_1 FOREIGN KEY (lo_id_login) REFERENCES users (lo_id))
And you've to add this constraint into USERS table.

Cannot add or update a child row: a foreign key constraint fails while adding another foreign key

I am trying to add a foreign key constraint in my table. My table structure are:
table requisition
My next table where i want to add foreign key of requisition table
requisition_approval
When i try to add a foreign key constraint with following sql query:
ALTER TABLE `requisition_approval` ADD CONSTRAINT `requisition_id` FOREIGN KEY (`requisition_id`) REFERENCES `requisition`(`id`) ON DELETE RESTRICT ON UPDATE RESTRICT;
It gives following error:
#1452 - Cannot add or update a child row: a foreign key constraint fails (`proprompt`.`#sql-34e8_3d7`, CONSTRAINT `requisition_id` FOREIGN KEY (`requisition_id`) REFERENCES `requisition` (`id`))
What is the problem here and how can i solve it?
Error is in the requisition_approval table more specificly in the requisition_id column. You inserted id 0 which does not references any id in requisition table. Change the value to 1 and this will work :)

Foreign Key Constraints

ERROR 1452: 1452: Cannot add or update a child row: a foreign key constraint fails (`harris`.`MANAGER`, CONSTRAINT `fk_EMPLOYEE_has_PROJECT_PROJECT1` FOREIGN KEY (`PROJECT_project_ID`) REFERENCES `PROJECT` (`project_ID`) ON DELETE NO ACTION ON UPDATE NO ACTION)
SQL Statement:
INSERT INTO `harris`.`MANAGER` (`EMPLOYEE_employee_ID`, `PROJECT_project_ID`) VALUES ('EMPLOYEE_employee_ID', 'PROJECT_project_ID')
No matter what foreign key I use it won't work. For some reason it adds a "1" to the end IE: PROJECT_PROJECT when it should just be project_id and employye_num

Always getting error with foreign key

Whenever I set a foreign key, I get the below error, and I'm not sure why. Can someone help me figure it out?
Error
SQL query: ALTER TABLE `vl_discount` ADD CONSTRAINT `vl_product_ibfk1` FOREIGN KEY ( `discountedProduct` )
REFERENCES `base`.`vl_product` (`autokey`) ON DELETE CASCADE ON UPDATE CASCADE ;
MySQL said: Documentation
#1452 - Cannot add or update a child row: a foreign key constraint fails
(`base`.`#sql41d_172`, CONSTRAINT `vl_product_ibfk1` FOREIGN KEY (`discountedProduct`)
REFERENCES `vl_product` (`autokey`) ON DELETE CASCADE ON UPDATE CASCADE)