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

Whenever I try to create a relationship between one table and another, to create a foreign key, i get the error:

#1452 - Cannot add or update a child row: a foreign key constraint fails (`expenses`.<result 2 when explaining filename '#sql-22f8_203'>, CONSTRAINT `#sql-22f8_203_ibfk_1` FOREIGN KEY (`users_id`) REFERENCES `users` (`users_id`))

Any ideas what this could be?  Googled this with no useful results. :(

Thanks.
LVL 1
LB1234Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

COBOLdinosaurCommented:
Without seeing the structure of the data and the code that is generation the error, it is not likely that we can do much more than speculate.

Cd&
0
LB1234Author Commented:
Error
SQL query:

ALTER TABLE  `transactions` ADD FOREIGN KEY (  `users_id` ) REFERENCES  `expenses`.`users` (

`users_id`
) ON DELETE RESTRICT ON UPDATE RESTRICT ;

Open in new window

0
LB1234Author Commented:
table
0
Newly released Acronis True Image 2019

In announcing the release of the 15th Anniversary Edition of Acronis True Image 2019, the company revealed that its artificial intelligence-based anti-ransomware technology – stopped more than 200,000 ransomware attacks on 150,000 customers last year.

Chris StanyonWebDevCommented:
Because you're adding a foreign key, you need to make sure that the data in the child table already exists in the parent table. In your case, every users_id that exists in the transactions table must already exist in the users table. The datatype has to match as well.
0
COBOLdinosaurCommented:
Okay you have an auto increment key and it is likely that some rows do not exist for every id. In that case the query fails because there is no value for the foreign key.

There is a closed mysql bug report which appears to have the code for an error handler that may help you.
http://bugs.mysql.com/bug.php?id=68831

Cd&
0
LB1234Author Commented:
I have exactly one user in the users_id with a value of "64" (previous test users were erased).  In the transactions table i have several dummy transactions and have a column called "users_id" which is populated with all zeroes.  Do I change these all to "64"?  The data type is the same.  Thanks
0
LB1234Author Commented:
COBOL, i'm not clear on which columns in which table your first statement refers.   Can you clarify?   thanks.
0
Chris StanyonWebDevCommented:
You do need to change the values - you can't have a record in a child table that doesn't have a matching record in a parent table - that's the whole point of a FOREIGN KEY. If you have 1 parent record with a user_id of 64, then the only records that are allowed in the child table must also have a user_id of 64.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LB1234Author Commented:
Thanks Chris and Cobol!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.