Himal
Himal

Reputation: 1371

ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails

When I execute the following SQL command:

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

I'm getting the following error message:

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

My tables:

CREATE TABLE IF NOT EXISTS `test_users` (
`userid` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(10) NOT NULL DEFAULT '',
`password` varchar(50) NOT NULL DEFAULT '',
PRIMARY KEY (userid)
) ENGINE=InnoDB;

INSERT INTO `test_users` (`userid`, `username`, `password) VALUES
(1120, 'guest', '12250170a9624f336ca24');

CREATE TABLE IF NOT EXISTS `test_hosts` (
`hid` int(11) NOT NULL AUTO_INCREMENT,
`UID` int(11) NOT NULL,
`name` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (hid)
) ENGINE=InnoDB;

INSERT INTO `test_hosts` (`hid`, `UID`, `name`) VALUES (30, 10, 'MU');

CREATE TABLE IF NOT EXISTS `test_usershosts` (
`RID` int(11) NULL AUTO_INCREMENT,
`userid` int(11) ,
`hid` int(11) ,
`Usr` varchar(100) ,
`Pass` varchar(100) ,
PRIMARY KEY (RID),
INDEX (userid),
INDEX (hid),
FOREIGN KEY (userid) REFERENCES test_users (userid) 
    ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (hid) REFERENCES test_hosts (hid) 
    ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB;

I've looked into many similar cases, but couldn't find any solution. Any help would be appreciated.

Upvotes: 13

Views: 47469

Answers (3)

ramiro
ramiro

Reputation: 1

in my case in case it serves someone. I was struggling to find the solution what usually happens when making a primary key is also marked as unique. but in my case it was only marked as primary and it was not marked as unique, until I noticed that the option of unica was "open" and I clicked it, until then I let myself make the relationship.

Then for the record ... check that your primary keys are also unique.

Upvotes: -1

w770115
w770115

Reputation: 187

In test_users guest's UserID is 1120 and in test_hosts hid = 30

you must change

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

to:

INSERT INTO test_usershosts (RID,userid,hid,Usr,Pass) VALUES (NULL,1120,30,"user","pass");

Upvotes: 2

John Woo
John Woo

Reputation: 263723

The reason why you are getting that exception is because you are inserting a record on table test_usershosts which the value of the userID is not present on table test_users. Same as the value of hid is not also present on table test_hosts.

Table test_usershosts is dependent on tables: test_users and test_hosts. So be sure that when inserting records on table test_usershosts, the values for hid and userid already exists on the parent tables: test_users and test_hosts.

Try executing this query and surely it will be inserted.

INSERT INTO test_usershosts (RID,userid,hid,Usr,Pass) 
VALUES (NULL,1120,30,'user','pass');

I see that AUTO_INCREMENT option on tables: test_users and test_hosts, are not needed since you are supplying values on every query you are executing on the two tables.

Upvotes: 23

Related Questions