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);
Related
I always get an error if I want to insert value in my tables..
SQL:
INSERT INTO `login`( `lo_password`, `lo_userName`, `lo_eMail`) VALUES ("sdsdf!D","Test!s1","test#test.com")
Error:
1452 - Cannot add or update a child row: a foreign key constraint fails (splitthebilldb.login, CONSTRAINT login_ibfk_1 FOREIGN KEY (lo_id) REFERENCES users (lo_id_login))
Your constraint is not in the right order :
CONSTRAINT login_ibfk_1 FOREIGN KEY (lo_id) REFERENCES users (lo_id_login))
should be :
CONSTRAINT login_ibfk_1 FOREIGN KEY (lo_id_login) REFERENCES users (lo_id))
And you've to add this constraint into USERS table.
I am trying to add a foreign key constraint in my table. My table structure are:
table requisition
My next table where i want to add foreign key of requisition table
requisition_approval
When i try to add a foreign key constraint with following sql query:
ALTER TABLE `requisition_approval` ADD CONSTRAINT `requisition_id` FOREIGN KEY (`requisition_id`) REFERENCES `requisition`(`id`) ON DELETE RESTRICT ON UPDATE RESTRICT;
It gives following error:
#1452 - Cannot add or update a child row: a foreign key constraint fails (`proprompt`.`#sql-34e8_3d7`, CONSTRAINT `requisition_id` FOREIGN KEY (`requisition_id`) REFERENCES `requisition` (`id`))
What is the problem here and how can i solve it?
Error is in the requisition_approval table more specificly in the requisition_id column. You inserted id 0 which does not references any id in requisition table. Change the value to 1 and this will work :)
I am using a MySQL database, have the following 3 tables:
map_range
map_location (join table)
location
I am trying to add some foreign keys.
ALTER TABLE `www`.`map_location`
ALTER TABLE `www`.`map_location`
ADD INDEX `fk_map_location_indx` (`LOC_ID` ASC);
ALTER TABLE `www`.`map_location`
ADD CONSTRAINT `fk_map_loc_map`
FOREIGN KEY (`MAP_ID`)
REFERENCES `www`.`map_location` (`MAP_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
ADD CONSTRAINT `fk_map_loc_location`
FOREIGN KEY (`LOC_ID`)
REFERENCES `www`.`location` (`ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
But I get the following error:
Operation failed: There was an error while applying the SQL script to the database.
ERROR 1025: Error on rename of '.\www\#sql-ea0_2b8' to '.\www\map_location' (errno: 150 - Foreign key constraint is incorrectly formed)
SQL Statement:
ALTER TABLE `www`.`map_location`
ADD CONSTRAINT `fk_map_loc_map`
FOREIGN KEY (`MAP_ID`)
REFERENCES `www`.`map_location` (`MAP_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
ADD CONSTRAINT `fk_map_loc_location`
FOREIGN KEY (`LOC_ID`)
REFERENCES `www`.`location` (`ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
Any ideas appreciated.
You are self-refferencing the same table and colum.
did you mean to location and not map_location?
ALTER TABLE `www`.`map_location`
ADD CONSTRAINT... FOREIGN KEY (`MAP_ID`)
REFERENCES `www`.`map_location` (`MAP_ID`)
I am creating foreign key but continuously getting error
1452 - Cannot add or update a child row: a foreign key constraint fails (demo_db.#sql-271c_ac, CONSTRAINT company_state_id FOREIGN
KEY (company_state_id) REFERENCES company (Id))
SQL query is
ALTER TABLE `state` ADD CONSTRAINT `company_state_id` FOREIGN KEY (`company_state_id`) REFERENCES `company`(`Id`) ON DELETE RESTRICT ON UPDATE RESTRICT;
Simply do this:
SET FOREIGN_KEY_CHECKS=0;
ALTER TABLE `state` ADD CONSTRAINT `company_state_id` FOREIGN KEY (`company_state_id`) REFERENCES `company`(`Id`) ON DELETE RESTRICT ON UPDATE RESTRICT;
SET FOREIGN_KEY_CHECKS=1;
This will first ignore all foreign key checks, alter your table and again restore the check to 1
Hope this helps :)
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.