Mysql Error Code 1452
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn error code 1452 mysql workbench more about Stack Overflow the company Business Learn more about hiring developers or mysql error 1452 foreign key constraint fails posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community a foreign key constraint fails mysql insert Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up ERROR 1452: Cannot add or update a child row: cannot add or update a child row a foreign key constraint fails on delete cascade on update cascade) a foreign key constraint fails up vote 31 down vote favorite 6 I have created tables in MySQL Workbench as shown below : ORDRE table: Create table Ordre( OrdreID int NOT NULL, OrdreDato date DEFAULT NULL, KundeID int DEFAULT NULL, constraint Ordre_pk primary key(OrdreID), constraint Ordre_fk foreign key(KundeID) references Kunde(KundeID) ) engine = InnoDB; PRODUKT table: Create table Produkt( ProduktID int NOT NULL, ProduktBeskrivelse varchar(100) DEFAULT NULL, ProduktFarge
Error Code 1452 Mysql Insert
varchar(20) DEFAULT NULL, Enhetpris int DEFAULT NULL, constraint Produkt_pk primary key(ProduktID) ) engine = InnoDB; and ORDRELINJE table: Create table Ordrelinje( Ordre int NOT NULL, Produkt int NOT NULL, AntallBestilt int DEFAULT NULL, constraint Ordrelinje_pk primary key(Ordre, Produkt), constraint Ordrelinje_fk foreign key(Ordre) references Ordre(OrdreID), constraint Ordrelinje_fk1 foreign key(Produkt) references Produkt(ProduktID) ) engine = InnoDB; so when I try to insert values into ORDRELINJE table i get : Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (srdjank.Ordrelinje, CONSTRAINT Ordrelinje_fk FOREIGN KEY (Ordre) REFERENCES Ordre (OrdreID)) I've seen the other posts on this topic, but no luck. Am I overseeing something or ... Any idea what to do? mysql mysql-error-1452 share|improve this question edited Feb 9 '14 at 14:34 Mihai 15.3k52542 asked Feb 9 '14 at 13:37 user3289677 156123 1 possible duplicate of Mysql error 1452 - Cannot add or update a child row: a foreign key constraint fails –Sagar Naliyapara Jul 16 '15 at 5:07 add a comment| 3 Answers 3 active oldest votes up vote 42 down vote Taken from Using FOREIGN KEY Constraints Foreign key relationships involve a parent table that holds the central data values, and a child table with identical valu
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about
Set Foreign_key_checks=0 Mysql
Stack Overflow the company Business Learn more about hiring developers or posting ads with mysql foreign key constraint fails us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is add foreign key to existing table mysql a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Mysql error 1452 - Cannot add or update a child row: a foreign key http://stackoverflow.com/questions/21659691/error-1452-cannot-add-or-update-a-child-row-a-foreign-key-constraint-fails constraint fails up vote 179 down vote favorite 53 I'm having a bit of a strange problem, I'm trying to add a foreign key to one table that references another, but it is failing for some reason. With my limited knowledge of MySQL, the only thing that could possibly be suspect is that there is a foreign key on a different table referencing the one I am trying to reference. http://stackoverflow.com/questions/1253459/mysql-error-1452-cannot-add-or-update-a-child-row-a-foreign-key-constraint-fa Here is a picture of my table relationships, generated via phpMyAdmin: Relationships I've done a SHOW CREATE TABLE query on both tables, sourcecodes_tags is the table with the foreign key, sourcecodes is the referenced table. CREATE TABLE `sourcecodes` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `user_id` int(11) unsigned NOT NULL, `language_id` int(11) unsigned NOT NULL, `category_id` int(11) unsigned NOT NULL, `title` varchar(40) CHARACTER SET utf8 NOT NULL, `description` text CHARACTER SET utf8 NOT NULL, `views` int(11) unsigned NOT NULL, `downloads` int(11) unsigned NOT NULL, `time_posted` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`), KEY `user_id` (`user_id`), KEY `language_id` (`language_id`), KEY `category_id` (`category_id`), CONSTRAINT `sourcecodes_ibfk_3` FOREIGN KEY (`language_id`) REFERENCES `languages` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `sourcecodes_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `sourcecodes_ibfk_2` FOREIGN KEY (`category_id`) REFERENCES `categories` (`id`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1 CREATE TABLE `sourcecodes_tags` ( `sourcecode_id` int(11) unsigned NOT NULL, `tag_id` int(11) unsigned NOT NULL, KEY `sourcecode_id` (`sourcecode_id`), KEY `tag_id` (`tag_id`), CONSTRAINT `sourcecodes_tags_ibfk_1` FOREIGN KEY (`tag_id`) REFERENCES `tags` (`id`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=latin1 It would be great if anyone could tell me what is going on here, I've had no formal trainin
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta http://stackoverflow.com/questions/21659691/error-1452-cannot-add-or-update-a-child-row-a-foreign-key-constraint-fails Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us http://dba.stackexchange.com/questions/25581/foreign-key-constraint-fails Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, foreign key helping each other. Join them; it only takes a minute: Sign up ERROR 1452: Cannot add or update a child row: a foreign key constraint fails up vote 31 down vote favorite 6 I have created tables in MySQL Workbench as shown below : ORDRE table: Create table Ordre( OrdreID int NOT NULL, OrdreDato date DEFAULT NULL, foreign key constraint KundeID int DEFAULT NULL, constraint Ordre_pk primary key(OrdreID), constraint Ordre_fk foreign key(KundeID) references Kunde(KundeID) ) engine = InnoDB; PRODUKT table: Create table Produkt( ProduktID int NOT NULL, ProduktBeskrivelse varchar(100) DEFAULT NULL, ProduktFarge varchar(20) DEFAULT NULL, Enhetpris int DEFAULT NULL, constraint Produkt_pk primary key(ProduktID) ) engine = InnoDB; and ORDRELINJE table: Create table Ordrelinje( Ordre int NOT NULL, Produkt int NOT NULL, AntallBestilt int DEFAULT NULL, constraint Ordrelinje_pk primary key(Ordre, Produkt), constraint Ordrelinje_fk foreign key(Ordre) references Ordre(OrdreID), constraint Ordrelinje_fk1 foreign key(Produkt) references Produkt(ProduktID) ) engine = InnoDB; so when I try to insert values into ORDRELINJE table i get : Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (srdjank.Ordrelinje, CONSTRAINT Ordrelinje_fk FOREIGN KEY (Ordre) REFERENCES Ordre (OrdreID)) I've seen the other posts on this topic, but no luck. Am I overseeing something or ... Any idea what to do? mysql mysql-error-1452 share|improve this question edited Feb 9 '14 at 14:34 Mihai 15.3k52542 asked Feb 9 '14 at 13:37 user3289677 156123 1 poss
log in tour help Tour Start here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Database Administrators Questions Tags Users Badges Unanswered Ask Question _ Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute: Sign up Here's how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top Foreign Key Constraint fails up vote 1 down vote favorite I have the following tables: // Base Scans CREATE TABLE `basescans` ( `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, `name` VARCHAR(100) NULL DEFAULT NULL, `status_id` INT(10) UNSIGNED NULL DEFAULT NULL, PRIMARY KEY (`id`), CONSTRAINT `status_id_fk` FOREIGN KEY (`status_id`) REFERENCES `statuses` (`id`) ON UPDATE CASCADE ON DELETE SET NULL ) COLLATE='utf8_general_ci' ENGINE=InnoDB ROW_FORMAT=COMPACT AUTO_INCREMENT=29 // Statuses CREATE TABLE `statuses` ( `id` INT(10) UNSIGNED NULL AUTO_INCREMENT, `name` VARCHAR(100) NULL DEFAULT NULL, PRIMARY KEY (`id`) ) COLLATE='utf8_general_ci' ENGINE=InnoDB ROW_FORMAT=DEFAULT AUTO_INCREMENT=4 Trying to save the first table fails when I put in that foreign key constraint. Can't figure out why. Both of the columns referenced in the constraint have the same type, size, etc: INT(10) UNSIGNED NULL They only have a difference default value. One has a default value of NULL, the other is AUTO_INCREMENT. I didn't think that made a difference for foreign key constraints but I could be wrong. Both tables are InnoDB and UFT8. What am I missing here? UPDATED: My specific error: /* SQL Error (1452): Cannot add or update a child row: a foreign key constraint fails (`db`.