MySQL Forums
Forum List  »  Newbie

Error Code: 1452 Cannot add or update a child row
Posted by: Eran Yasso
Date: July 19, 2018 10:44AM

Hi,

For some reason I've started to get this error:
Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails
(`SoLMySqlDB`.`orders`, CONSTRAINT `fk_orders_users1` FOREIGN KEY (`users_user_id`) REFERENCES `users` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE)
When I running this command:
INSERT INTO orders(orderPosted, datePosted, lastUpdated, status, users_user_id) VALUES ("ddsfsdf", "2018-07-14 10:38:06", "2018-07-14 10:38:06", 1, 2);

I have users table and user_id 2.
UPDATE works: UPDATE orders set orderPosted = "dfdfdfdf" WHERE order_id = 101;

I deleted all the records in orders table but it still happens.
the relationship between users table and orders table is one to many.

This is the scheme for orders table
DROP TABLE IF EXISTS `orders`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `orders` (
`order_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`users_user_id` int(10) unsigned NOT NULL,
`orderPosted` text NOT NULL,
`datePosted` datetime NOT NULL,
`finalizedOrder` text,
`finalizedOrderPosted` datetime DEFAULT NULL,
`lastUpdated` datetime NOT NULL,
`status` enum('received','processed','in_process','canceled','edited','approved') NOT NULL DEFAULT 'received',
PRIMARY KEY (`order_id`,`users_user_id`),
KEY `fk_orders_users1_idx` (`users_user_id`),
CONSTRAINT `fk_orders_users1` FOREIGN KEY (`users_user_id`) REFERENCES `users` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=110 DEFAULT CHARSET=utf8;


and scheme for users table:
DROP TABLE IF EXISTS `users`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `users` (
`user_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`userName` varchar(20) NOT NULL,
`password` varchar(255) NOT NULL,
`salt` varchar(30) NOT NULL,
`name` varchar(45) NOT NULL,
`contactPerson` varchar(255) DEFAULT NULL,
`email` varchar(255) DEFAULT NULL,
`mobile` varchar(20) NOT NULL,
`phoneNumber` varchar(20) DEFAULT NULL,
`isActive` bit(1) NOT NULL DEFAULT b'0',
`vatNumber` varchar(45) DEFAULT NULL,
`userType` int(10) NOT NULL,
`solRevenue` double NOT NULL DEFAULT '3',
`city` varchar(45) DEFAULT NULL,
`street` varchar(45) DEFAULT NULL,
`streetNumber` int(10) unsigned DEFAULT NULL,
`deviceToken` varchar(255) DEFAULT NULL,
PRIMARY KEY (`user_id`),
UNIQUE KEY `userName` (`userName`),
UNIQUE KEY `mobile` (`mobile`),
UNIQUE KEY `vatNumber` (`vatNumber`),
UNIQUE KEY `email` (`email`),
UNIQUE KEY `phoneNumber` (`phoneNumber`),
UNIQUE KEY `address_UNIQUE` (`streetNumber`,`street`,`city`),
UNIQUE KEY `deviceToken_UNIQUE` (`deviceToken`),
KEY `name_index` (`name`),
KEY `city_index` (`city`)
) ENGINE=InnoDB AUTO_INCREMENT=38 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;


Any idea when causing this?
Thanks,

Options: ReplyQuote




Sorry, only registered users may post in this forum.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.