I'm creating a DB in MySQL Workbench. I created the schemas in an EER Diagram (in Windows), moved to Linux after that, and I did Forward Engineering for getting the SQL Script. Now I'm testing the DB, but I'm getting this error in something that should be pretty straightforward as adding a row in the child of a one-to-one relationship.
These are the insert statements:
INSERT INTO `DB_local`.`User` (`idUser`, `name`, `surname`, `gender`, `bornDate`, `address`, `city`, `postcode`, `telephone1`, `telephone2`, `email`) VALUES ('1', '1', '1', '1', '2020/01/01', '1', '1', '1', '1', '1', '1');
That ran OK
INSERT INTO `DB_local`.`UserAdditionalDetails` (`idUser`, `cardNumber`, `userHash`, `entryDate`, `member`, `section`) VALUES ('1', '1', '1', '2020/01/01', '1', '1');
That got the error:
ERROR 1452: 1452: Cannot add or update a child row: a foreign key constraint fails (
DB_local.UserAdditionalDetails, CONSTRAINTfk_UserAdditionalDetails_User1FOREIGN KEY (idUser) REFERENCESUser(idUser) ON DELETE NO ACTION ON UPDATE NO ACTION)
Any idea of why this simple INSERT is not working as expected?
I searched it, and I saw in other questions people suggesting to "move" the AdditionalDetails data to the User table (that I would prefer to avoid, for design reasons), or to disable the referencial integrity before inserting in that table (that it's a hack, because it's something that should work fine).
For adding more context, this is the code generated to create that part of the DB (the DB is much more complex):
-- MySQL Workbench Forward Engineering
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
-- -----------------------------------------------------
-- Schema DB_local
-- -----------------------------------------------------
DROP SCHEMA IF EXISTS `DB_local` ;
-- -----------------------------------------------------
-- Schema DB_local
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `DB_local` DEFAULT CHARACTER SET utf8 ;
USE `DB_local` ;
-- -----------------------------------------------------
-- Table `DB_local`.`User`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `DB_local`.`User` ;
CREATE TABLE IF NOT EXISTS `DB_local`.`User` (
`idUser` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(25) NOT NULL,
`surname` VARCHAR(75) NULL,
`gender` VARCHAR(25) NULL,
`bornDate` DATE NULL,
`address` VARCHAR(300) NULL,
`city` VARCHAR(50) NULL,
`postcode` VARCHAR(5) NULL,
`telephone1` VARCHAR(10) NULL,
`telephone2` VARCHAR(10) NULL,
`email` VARCHAR(45) NOT NULL,
PRIMARY KEY (`idUser`))
ENGINE = MyISAM;
-- -----------------------------------------------------
-- Table `DB_local`.`UserAdditionalDetails`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `DB_local`.`UserAdditionalDetails` ;
CREATE TABLE IF NOT EXISTS `DB_local`.`UserAdditionalDetails` (
`idUser` INT NOT NULL,
`cardNumber` VARCHAR(12) NOT NULL,
`userHash` VARCHAR(45) NOT NULL,
`entryDate` DATE NOT NULL,
`member` VARCHAR(45) NULL,
`section` VARCHAR(45) NULL,
PRIMARY KEY (`idUser`),
INDEX `fk_UserAdditionalDetails_User1_idx` (`idUser` ASC),
CONSTRAINT `fk_UserAdditionalDetails_User1`
FOREIGN KEY (`idUser`)
REFERENCES `DB_local`.`User` (`idUser`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
... MORE TABLES ...
SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
Finally, seems that the issue was that I have some tables created with MyISAM and other ones created with InnoDB. Probably caused by the linux migration. I updated the Engine to InnoDB in all the tables and it's working as expected.