• Register
0 votes
53 views

Problem :

When I try to execute the below SQL command :

INSERT INTO mytest_usershosts (RID,userid,hid,Usr,Pass) 
VALUES (NULL,1,1,"user","pass");

I am facing the below error message :

ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint f
ails (`dm`.`mytest_usershosts`, CONSTRAINT `mytest_usershosts_ibfk_1` FOREIGN KEY (`
userid`) REFERENCES `mytest_users` (`userid`) ON DELETE CASCADE ON UPDATE CASCADE)

 

I have already looked into many similar cases, but I am unable to find any solution.

Is there anyone who have faced the same issue and knows the fix for the issue?

6 5 3
7,540 points

1 Answer

0 votes

Solution :

The reason behind you facing that error is that you are trying to insert the record on the table mytest_usershosts but the value of the userID is not present on your table mytest_users. Same for the value of hid it is also not present on table mytest_hosts.

Table mytest_usershosts is dependent on the tables: mytest_users and mytest_hosts. So please make sure that while inserting the records on the table mytest_usershosts, the values for hid and userid should already be available on the following parent tables: mytest_users and mytest_hosts.

Now try executing below query and surely it will be inserted.

INSERT INTO mytest_usershosts (RID,userid,hid,Usr,Pass)

VALUES (NULL,1120,30,'user','pass');

Also the  AUTO_INCREMENT option on the tables: mytest_users and mytest_hosts, are needless as you are supplying the values on every query that you are executing on the two tables.

9 7 4
38,600 points

Related questions

0 votes
2 answers 557 views
557 views
Problem : I have created the tables in MySQL Workbench. But when I try to insert the values into the ORDRELINJE table i get below error: Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (srdjank.Ordrelinje, CONSTRAINT Ordrelinje_fk FOREIGN KEY ... (OrdreID)) I have seen many other posts on this topic but unable to fix my issue. How can I fix this issue?
asked Jan 24 jwilliam 3.9k points
0 votes
2 answers 193 views
193 views
Problem : I have created the tables in MySQL Workbench. But when want to insert values into my TBL ORDRELINJE table I face below error : Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (srdjank.TBLOrdrelinje, CONSTRAINT TBLOrdrelinje_fk FOREIGN KEY ... do the research on this topic, but no luck. Am I doing any mistake or someone have any idea about what can I do?
asked Dec 21, 2019 alecxe 7.5k points
0 votes
1 answer 156 views
156 views
Problem : In the MySQL I am trying to drop the table named bericht. I tried many options however still facing the same error that my table cannot be dropped. I am facing following error message : #1217 - Cannot delete or update a parent row: a foreign key constraint fails Does anybody have any clue about this error #1217?
asked Dec 2, 2019 alecxe 7.5k points
0 votes
1 answer 11 views
11 views
Problem: I am unable to understand why a TRUNCATE on mygroup is not working? Even though I have it ON DELETE CASCADE SET I encounter below strange error: ERROR 1701 (42000): Cannot truncate a table referenced in a foreign key constraint (mytest.instance, ...    DateTime     DATETIME DEFAULT NULL, FOREIGN KEY  (GroupID) REFERENCES mygroup(ID) ON DELETE CASCADE,    UNIQUE(GroupID) ) ENGINE=InnoDB;
asked Jul 23 Raphael Pacheco 4.9k points
0 votes
1 answer 25 views
25 views
I am facing this problems in many times and hardly getting the soluton. Can someone here help me to solve this issue?
asked Sep 14 Daniel Anderson 4k points