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 :)
Related
I'm trying to have a foreign key (parent_id) and a primary key (id) in an already existing table.
I already used SET FOREIGN_KEY_CHECKS=0; and then I ran this query below but every time I get the same error.
ALTER TABLE categories
ADD CONSTRAINT fk_parent_id FOREIGN KEY ( parent_id ) REFERENCES
categories(id) on DELETE CASCADE
Cannot add or update a child row: a foreign key constraint fails
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 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.
I have a foreign key in 3 tables and they are on cascade update . so I would like to add another cascade delete ,. Is that possible without dropping the constraint ? if yes please give me any example with alter .
To change your foreign key, you first have to drop it (using the name) and then create a new foreign key with the correct definition and your done!
ALTER TABLE `pets` DROP FOREIGN KEY `your_fk_name_here`;
ALTER TABLE `pets` ADD FOREIGN KEY (`owner_id`) REFERENCES `owners`(`id`) ON UPDATE CASCADE ON DELETE CASCADE;
or
ALTER TABLE `pets` ADD CONSTRAINT fk_owner_pet FOREIGN KEY (`owner_id`) REFERENCES `owners`(`id`) ON UPDATE CASCADE ON DELETE CASCADE;