Always getting error with foreign key - mysql

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)

Related

I want to add an foreign key but everytime i try adding one i get error codes

Coding that i used to altar my table and change it.
ALTER TABLE `the-challenge`.`klant`
ADD CONSTRAINT `resultaat`
FOREIGN KEY (`resultaatid`)
REFERENCES `the-challenge`.`resultaat` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
The results when trying to apply the code:
Operation failed: There was an error while applying the SQL script to the database.
Executing:
ALTER TABLE `the-challenge`.`klant`
ADD CONSTRAINT `resultaat`
FOREIGN KEY (`resultaatid`)
REFERENCES `the-challenge`.`resultaat` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
ERROR 1826: Duplicate foreign key constraint name 'resultaat'
SQL Statement:
ALTER TABLE `the-challenge`.`klant`
ADD CONSTRAINT `resultaat`
FOREIGN KEY (`resultaatid`)
REFERENCES `the-challenge`.`resultaat` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION

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 :)

Foreign key constraint fails in phpmyadmin while creating foreign key

I am creating foreign key but continuously getting error
1452 - Cannot add or update a child row: a foreign key constraint fails (demo_db.#sql-271c_ac, CONSTRAINT company_state_id FOREIGN
KEY (company_state_id) REFERENCES company (Id))
SQL query is
ALTER TABLE `state` ADD CONSTRAINT `company_state_id` FOREIGN KEY (`company_state_id`) REFERENCES `company`(`Id`) ON DELETE RESTRICT ON UPDATE RESTRICT;
Simply do this:
SET FOREIGN_KEY_CHECKS=0;
ALTER TABLE `state` ADD CONSTRAINT `company_state_id` FOREIGN KEY (`company_state_id`) REFERENCES `company`(`Id`) ON DELETE RESTRICT ON UPDATE RESTRICT;
SET FOREIGN_KEY_CHECKS=1;
This will first ignore all foreign key checks, alter your table and again restore the check to 1
Hope this helps :)

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

error code #1452 when adding foreign keys

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)