Cita:
Al insertar un nuevo dato en la segunda tabla, la que tiene el FK al ID de la primera tabla, me tira este error: 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';
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`Login`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Login` (
`id_login` INT NOT NULL AUTO_INCREMENT ,
`email` VARCHAR(40) NOT NULL ,
`password` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`id_login`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Question`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Question` (
`id_question` INT NOT NULL AUTO_INCREMENT ,
`qstns` CHAR(100) NOT NULL ,
`modificado` TIMESTAMP NOT NULL ,
`id_fk` INT NOT NULL ,
PRIMARY KEY (`id_question`) ,
INDEX `id_fk` (`id_fk` ASC) ,
CONSTRAINT `id_fk`
FOREIGN KEY (`id_fk` )
REFERENCES `mydb`.`Login` (`id_login` )
ON DELETE NO ACTION
ON UPDATE CASCADE)
ENGINE = InnoDB;
SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL';
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`Login`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Login` (
`id_login` INT NOT NULL AUTO_INCREMENT ,
`email` VARCHAR(40) NOT NULL ,
`password` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`id_login`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Question`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Question` (
`id_question` INT NOT NULL AUTO_INCREMENT ,
`qstns` CHAR(100) NOT NULL ,
`modificado` TIMESTAMP NOT NULL ,
`id_fk` INT NOT NULL ,
PRIMARY KEY (`id_question`) ,
INDEX `id_fk` (`id_fk` ASC) ,
CONSTRAINT `id_fk`
FOREIGN KEY (`id_fk` )
REFERENCES `mydb`.`Login` (`id_login` )
ON DELETE NO ACTION
ON UPDATE CASCADE)
ENGINE = InnoDB;
SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
Cita:
He estado leyendo mucho sobre este error y veo que es porque intento meter algo que no existe en la tabla padre :SCannot add or update a child row: a foreign key constraint fails (`mydb`.`qstns`, CONSTRAINT `qstns_ibfk_1` FOREIGN KEY (`id_fk`) REFERENCES `login` (`id_login`))
Qué pasa? cómo lo arreglo?