Mysql Error Number 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 error code 1452 mysql workbench of this site About Us Learn more about Stack Overflow the company Business mysql error 1452 foreign key constraint fails 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
Error Code 1452 Mysql Insert
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, `
here for a quick overview of the site Help Center Detailed answers to any questions you might
Mysql Replication Error 1452
have Meta Discuss the workings and policies of this site About set foreign_key_checks=0 mysql Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads er_no_referenced_row_2 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 http://stackoverflow.com/questions/1253459/mysql-error-1452-cannot-add-or-update-a-child-row-a-foreign-key-constraint-fa programmers, just like you, helping each other. Join them; it only takes a minute: Sign up MySQL Foreign key constraint - Error 1452 - Cannot add or update child row up vote 3 down vote favorite I've used the other posts on this topic, but I'm having no luck. Here's the code I execute: UPDATE http://stackoverflow.com/questions/14222967/mysql-foreign-key-constraint-error-1452-cannot-add-or-update-child-row tblOrderItems SET `ItemID` = 0004 WHERE `OrderNum`= 203 AND `OrderItemID` = 26 Here's my error: Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (`cai0066`.`tblOrderItems`, CONSTRAINT `ItemID` FOREIGN KEY (`ItemID`) REFERENCES `tblCatalogItems` (`ItemID`)) Notes: It happens when I either INSERT or UPDATE into tblOrderItems. tblCatalogItems does have an ItemID of 0004. See: this Here are the create statements generated by MySQL Workbench: delimiter $$ CREATE TABLE `tblCatalogItems` ( `ItemID` varchar(10) NOT NULL DEFAULT '', `ItemName` varchar(50) DEFAULT NULL, `Wholesale` decimal(10,2) DEFAULT NULL, `Cost5-10` decimal(10,2) DEFAULT NULL, `Cost11-19` decimal(10,2) DEFAULT NULL, `Cost20` decimal(10,2) DEFAULT NULL, `Retail` decimal(10,2) DEFAULT NULL, PRIMARY KEY (`ItemID`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1$$ delimiter $$ CREATE TABLE `tblItemCosts` ( `Cost` decimal(10,2) DEFAULT NULL, `VendorID` int(11) NOT NULL, `ItemID` varchar(10) NOT NULL, KEY `VendorID_idx` (`VendorID`), KEY `ItemID_idx` (`ItemID`), CONSTRAINT `VendorID` FOREIGN KEY (`VendorID`) REFERENCES `tblVendors` (`VendorID`) ON DELETE NO ACTION ON UPDATE NO ACTION ) ENGINE=InnoDB DEFAULT CHARSET=latin1$$ delimiter $$ CREATE TABLE `tblOrderItems` ( `OrderItemID` int(11) NO
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 http://stackoverflow.com/questions/2703257/mysql-error-1452-cannot-add-or-update-a-child-row-a-foreign-key-constraint-fa 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, https://www.experts-exchange.com/questions/28238845/1452-Cannot-add-or-update-a-child-row-a-foreign-key-constraint-fails.html 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 constraint fails up vote 3 down vote favorite foreign key I've looked at other people's questions on this topic but can't seem to find where my error is coming from. Any help would be greatly appreciated. I'm including as much as I can think of that might help find the problem: CREATE TABLE stocks ( id INT AUTO_INCREMENT NOT NULL, user_id INT(11) UNSIGNED NOT NULL, ticker VARCHAR(20) NOT NULL, name VARCHAR(20), rating INT(11), position ENUM("strong buy", "buy", "sell", "strong sell", "neutral"), next_look DATE, privacy foreign key constraint ENUM("public", "private"), PRIMARY KEY(id), FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE IF NOT EXISTS `stocks_tags` ( `stock_id` INT NOT NULL, `tag_id` INT NOT NULL, PRIMARY KEY (`stock_id`,`tag_id`), KEY `fk_stock_tag` (`tag_id`), KEY `fk_tag_stock` (`stock_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ALTER TABLE `stocks_tags` ADD CONSTRAINT `fk_stock_tag` FOREIGN KEY (`tag_id`) REFERENCES `tags` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, ADD CONSTRAINT `fk_tag_stock` FOREIGN KEY (`stock_id`) REFERENCES `stocks` (`id`) ON DELETE CASCADE ON UPDATE CASCADE; CREATE TABLE tags( id INT AUTO_INCREMENT NOT NULL PRIMARY KEY, tags VARCHAR(30), UNIQUE(tags) ) ENGINE=INNODB DEFAULT CHARSET=utf8; And the error I'm getting: Database_Exception [ 1452 ]: Cannot add or update a child row: a foreign key constraint fails (`ddmachine`.`stocks_tags`, CONSTRAINT `fk_stock_tag` FOREIGN KEY (`tag_id`) REFERENCES `tags` (`id`) ON DELETE CASCADE ON UPDATE CASCADE) [ INSERT INTO `stocks_tags` (`stock_id`, `tag_id`) VALUES (19, 'cash') ] I did see that someone else had a similar problem based on their enum columns but don't think that's it. mysql database foreign-keys share|improve this question asked Apr 24 '10 at 4:46 dscher 82011123 add a comment| 1 Answer 1 active oldest votes up vote 4 down vote accepted It means that the tag_id cash is missing from the tag table... shouldn't tag_id be an integer? (in this query I mean) INSERT INTO `stocks_tags` (`stock_id`, `tag_id`) VALUES (19, 'cash')
for Help Receive Real-Time Help Create a Freelance Project Hire for a Full Time Job Ways to Get Help Ask a Question Ask for Help Receive Real-Time Help Create a Freelance Project Hire for a Full Time Job Ways to Get Help Expand Search Submit Close Search Login Join Today Products BackProducts Gigs Live Careers Vendor Services Groups Website Testing Store Headlines Experts Exchange > Questions > #1452 - Cannot add or update a child row: a foreign key constraint fails Want to Advertise Here? Solved #1452 - Cannot add or update a child row: a foreign key constraint fails Posted on 2013-09-13 PHP MySQL Server 2 Verified Solutions 9 Comments 15,370 Views Last Modified: 2013-09-13 Whenever I try to create a relationship between one table and another, to create a foreign key, i get the error: #1452 - Cannot add or update a child row: a foreign key constraint fails (`expenses`.