Foreign key constraint fails in an empty table - mysql

I have a new project running, where there are 2 tables groups and members. They are both empty at the time and there is a foreing key constraint on members table group_id.
Now I start off by creating a group which may later have many members, however, as I try to create it, I receive the following message:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`cogro`.`groups`, CONSTRAINT `groups_ibfk_1` FOREIGN KEY (`id`) REFERENCES `members` (`group_id`) ON DELETE NO ACTION ON UPDATE NO ACTION)
Any help or guidance is much appreciated.

You have entered wrong direction of the foreign key. Currently groups.id references members.group_id (in order to have an entry in groups with id = 1 you should have an entry in members with group_id = 1)
Change the defintion to:
CONSTRAINT `members_group_id_fk` FOREIGN KEY (`group_id`) REFERENCES `groups` (`id`)

Related

SQL FK constraint fails

I have 2 tables, Office and User. I want to make relation OneToMany (1 office has many users).
But when i run this sql
ALTER TABLE izo_user ADD CONSTRAINT FK_DA8075CFFA0C224 FOREIGN KEY (office_id) REFERENCES izo_office (id)
CREATE INDEX IDX_DA8075CFFA0C224 ON izo_user (office_id)
something goes wrong and i get error
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`izoplast`.`#sql-9842_1be9`, CONSTRAINT `FK_DA8075CFFA0C224` FOREIGN KEY (`office_id`) REFERENCES `izo_office` (`id`))
My Tables: http://oi57.tinypic.com/whhezr.jpg
try it if it works for you-
ALTER TABLE izo_user add index idx_office_id(office_id);
ALTER TABLE izo_user ADD CONSTRAINT FK_DA8075CFFA0C224 FOREIGN KEY (office_id) REFERENCES izo_office (id);

Error 1215: cannot add foreign key constraint in my SQL

I have the parent table which is gym_member, and I have the child which is medical_history.
In gym_member table there are two primary key which are gm_id and student_id.
In medical history table there are one primary which is mh_id, and I want to add a foreign key which is student_id but it show me this error.
ALTER TABLE `hct_gym`.`medical_history`
ADD CONSTRAINT `student_id`
FOREIGN KEY (`student_id`)
REFERENCES `hct_gym`.`gym_member` (`student_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
Operation failed: There was an error while applying the SQL script to the database.
ERROR 1215: Cannot add foreign key constraint
SQL Statement:
ALTER TABLE `hct_gym`.`medical_history`
ADD CONSTRAINT `student_id`
FOREIGN KEY (`student_id`)
REFERENCES `hct_gym`.`gym_member` (`student_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
I want to know where is the problem?
Check if column medical_history.student_id contains values
which are not contained in gym_member.student_id
That would contradict the constraint before /while it is being created.

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)

Foreign key error 1451 MySQL on updating IDs to "move" entry

I have recently migrated a system to a new server and using InnoDB tables for all and I've come up against some foreign key constraint issues. Most I fixed, but I'm stuck on one. The system (java webapp) has always handled the foreign key stuff on its own so this is a bit new, but fine with me as long as I get it working.
There are 4 hierarchical tables macro_client, client, client_location, and client_contact... so a macro_client can have many clients and so on. client_contact has foreign keys for the previous 3. And there is a cases table, and they are connected by an m:n table.
One task the system performs is to move parts of a new record to an existing one - like moving a new client_contact and client_location under a previously existing client and macro_client. This is done when processing a submission and someone finds the client already exists in the system - system makes this easy. So the system changes the ids to do that (then removes the dangling client and macro_client no longer needed). This is where the error comes in.
Here is the error I receive. I have run these update statements manually in MySQL Workbench one at a time and I get the same message. It happens on the first update I do, no matter which order I've tried.
Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`prod1_cases_clients`.`client_contact_cases`, CONSTRAINT `fk_{345FCA29-D80B-48E0-A25E-06432436F9A6}` FOREIGN KEY (`client_contact_id`, `client_loc_id`, `client_id`, `macro_client_id`) REFERENC)
The updates are
UPDATE client_contact SET macro_client_id = 61, client_id = 55 WHERE macro_client_id = 13368;
UPDATE client_location SET macro_client_id = 61, client_id = 55 WHERE macro_client_id = 13368;
UPDATE client_contact_cases SET macro_client_id = 61, client_id = 55 WHERE case_id = 1286148;
Am I going about this wrong? Do I need to change the foreign key behavior? I'd prefer the webapp handle the foreign key updates since it already does and to keep portability. I just need to get it working within the foreign key mentality I guess. Otherwise I'll just need to turn the foreign key checks off and on each time. Or maybe that's my only choice?
Thanks for any help!
UDPATED IMAGE AND ADDED SHOW CREATE TABLE
You can see the obvious circle, but it's not one-to-many all the way around. I can remove the link between client_contact and package - it's not really being used.
CREATE TABLE `macro_client` (...,
PRIMARY KEY (`macro_client_id`)
)
CREATE TABLE `client` (...,
PRIMARY KEY (`client_id`,`macro_client_id`),
KEY `client_FKIndex1` (`macro_client_id`),
KEY `index_client_referrer_form_url_name` (`referrer_form_url_name`),
CONSTRAINT `fk_{3CBF0AA2-914E-49DF-930D-8DA10CA5DDA7}` FOREIGN KEY (`macro_client_id`) REFERENCES `macro_client` (`macro_client_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
)
CREATE TABLE `client_location` (...,
PRIMARY KEY (`client_loc_id`,`client_id`,`macro_client_id`),
KEY `client_location_FKIndex1` (`client_id`,`macro_client_id`),
CONSTRAINT `fk_{98D38761-65BA-4FC1-9BC9-2B6B95B23394}` FOREIGN KEY (`client_id`, `macro_client_id`) REFERENCES `client` (`client_id`, `macro_client_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
)
CREATE TABLE `client_contact` (...,
PRIMARY KEY (`client_contact_id`,`client_loc_id`,`client_id`,`macro_client_id`),
UNIQUE KEY `weblogin_user_UNIQUE` (`weblogin_user`),
KEY `client_contact_FKIndex1` (`client_loc_id`,`client_id`,`macro_client_id`),
CONSTRAINT `fk_{B0C4B667-CDD6-4DEF-BC06-31C7C9BB29C2}` FOREIGN KEY (`client_loc_id`, `client_id`, `macro_client_id`) REFERENCES `client_location` (`client_loc_id`, `client_id`, `macro_client_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
)
CREATE TABLE `client_contact_cases` (...,
PRIMARY KEY (`macro_client_id`,`client_id`,`client_loc_id`,`client_contact_id`,`case_id`),
KEY `client_contact_has_cases_FKIndex1` (`client_contact_id`,`client_loc_id`,`client_id`,`macro_client_id`),
KEY `client_contact_has_cases_FKIndex2` (`case_id`),
CONSTRAINT `fk_{345FCA29-D80B-48E0-A25E-06432436F9A6}` FOREIGN KEY (`client_contact_id`, `client_loc_id`, `client_id`, `macro_client_id`) REFERENCES `client_contact` (`client_contact_id`, `client_loc_id`, `client_id`, `macro_client_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_{1C2AE8A5-3D40-4827-99CD-AC1D3A2EE4C9}` FOREIGN KEY (`case_id`) REFERENCES `cases` (`case_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
)
CREATE TABLE `cases` (...,
PRIMARY KEY (`case_id`),
KEY `index_cases_case_mgr` (`case_mgr`),
KEY `index_cases_status_code` (`status_code`)
)
CREATE TABLE `package` (...,
PRIMARY KEY (`package_id`,`macro_client_id`,`client_id`,`client_loc_id`,`client_contact_id`),
KEY `package_FKIndex1` (`client_contact_id`,`client_loc_id`,`client_id`,`macro_client_id`),
KEY `package_loc_id_index` (`loc_id`),
KEY `package_package_logger_index` (`package_logger`),
KEY `package_recd_sent_date_index` (`recd_sent_date`),
CONSTRAINT `fk_{37F3D2D5-0D52-4199-B963-34FEE1AD3F2F}` FOREIGN KEY (`client_contact_id`, `client_loc_id`, `client_id`, `macro_client_id`) REFERENCES `client_contact` (`client_contact_id`, `client_loc_id`, `client_id`, `macro_client_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=1343 DEFAULT CHARSET=utf8 PACK_KEYS=0
CREATE TABLE `item` (...,
PRIMARY KEY (`item_id`,`package_id`,`case_id`,`macro_client_id`,`client_id`,`client_loc_id`,`client_contact_id`),
KEY `item_FKIndex1` (`package_id`,`macro_client_id`,`client_id`,`client_loc_id`,`client_contact_id`),
KEY `item_FKIndex2` (`case_id`),
CONSTRAINT `fk_{2FD3D784-5AFC-47A6-A836-5A421A603B6D}` FOREIGN KEY (`package_id`, `macro_client_id`, `client_id`, `client_loc_id`, `client_contact_id`) REFERENCES `package` (`package_id`, `macro_client_id`, `client_id`, `client_loc_id`, `client_contact_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_{30C849E2-6843-4815-8FD5-0F1C21C6976B}` FOREIGN KEY (`case_id`) REFERENCES `cases` (`case_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
)

error adding a foreign key constraint

I have the following query:
ALTER TABLE ROUTE ADD FOREIGN KEY (RID) REFERENCES RESERVATION(RID) ON DELETE CASCADE
but it generates me an error:
#1452 - Cannot add or update a child row: a foreign key constraint fails (`SmarTrek`.`#sql-91e_d09`, CONSTRAINT `FK_RID` FOREIGN KEY (`RID`) REFERENCES `RESERVATION` (`RID`) ON DELETE CASCADE)
In designer mode, here's what it looks like:
That would meant that you already have data in the ROUTE table that does not satisfy the foreign key constraint.
To find the offending records, so you can update them to some other value (that exists), you can use
select *
from route
where rid not in (select rid from reservation)
THERE may b 2 reasons
there may b some row in ROUTE TABLE which have RID that does not exist in RESERVATION(RID)
or check the DATATYPE OF ROUTE (RID) & RESERVATION(RID) both should be same ( unsigned/signed)