Jeevan Manik Reddy
Jeevan Manik Reddy

Reputation: 13

Parent table cannot be updated when foreign key is present

There are two tables one is a parent i.e., groups table which has foreign key to a child table i.e., users. I am not able to edit foreign key column in parent table where as I have given it to cascade to child table. It gives a error as follows:

Error Code : 1452
Cannot add or update a child row: a foreign key constraint fails (`tms`.`groups`, CONSTRAINT `FK_groups` FOREIGN KEY (`GroupName`) REFERENCES `users` (`groupname`) ON DELETE CASCADE ON UPDATE CASCADE)

Thanks, -Jeevan

Upvotes: 0

Views: 98

Answers (2)

My-Name-Is
My-Name-Is

Reputation: 4942

This happens if you try to reference a non existing entry in database. In short, you inserted into groups and tried to reference an user entry which doesn't exist yet.

Upvotes: 0

RandomSeed
RandomSeed

Reputation: 29769

I assume a group contains many users, and a users belongs to one group.

Then you have declared the foreign key in the wrong direction. Actually users.groupname must reference tms.groups. Drop the current foreign key and rebuild it the other way around (in the users table).

Upvotes: 1

Related Questions