Mysql Add Foreign Key Error 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
Mysql Error 1452 Foreign Key Constraint Fails
of this site About Us Learn more about Stack Overflow the company Business error code 1452 mysql workbench Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges a foreign key constraint fails mysql insert Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute:
Cannot Add Or Update A Child Row A Foreign Key Constraint Fails On Delete Cascade On Update Cascade)
Sign up Mysql error 1452 - Cannot add or update a child row: a foreign key 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
Mysql Replication Error 1452
could possibly be suspect is that there is a foreign key on a different table referencing the one I am trying to reference. 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`
here for a quick overview of the site Help Center Detailed answers to any questions set foreign_key_checks=0 mysql you might have Meta Discuss the workings and policies of this
Er_no_referenced_row_2
site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers mysql foreign key constraint fails or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a http://stackoverflow.com/questions/1253459/mysql-error-1452-cannot-add-or-update-a-child-row-a-foreign-key-constraint-fa 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: a foreign key constraint fails up vote 31 down vote favorite 6 I have created tables in MySQL Workbench as shown below : ORDRE table: http://stackoverflow.com/questions/21659691/error-1452-cannot-add-or-update-a-child-row-a-foreign-key-constraint-fails 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 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-
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 http://stackoverflow.com/questions/5506798/error-1452-when-altering-mysql-table-to-add-foreign-key more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us 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, helping each other. Join them; it only takes a minute: Sign up Error #1452 when altering mySQL table to add foreign key foreign key up vote 1 down vote favorite Trying to help an intern with her project. She wants to add foreign keys to an existing table but this query: ALTER TABLE `document` ADD CONSTRAINT `document_ibfk_1` FOREIGN KEY (`cle_author`) REFERENCES `author` (`id_author`) ON DELETE CASCADE ON UPDATE CASCADE; gives this error: ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`wrc_mysql`.