MySQL - 外键约束错误

时间:2011-11-19 00:45:40

标签: mysql foreign-keys constraints

我在论坛上看到了这个问题,但我似乎无法解决我的特殊情况。 我在一个简单的插入上遇到以下错误。

Cannot add or update a child row: a foreign key constraint fails (`bag_track`.`job`, CONSTRAINT `fk_JOB_FLIGHT1` FOREIGN KEY (`FLIGHT_ID`) REFERENCES `FLIGHT` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION)

以下是我的表定义: 飞行表

CREATE  TABLE IF NOT EXISTS `BAG_TRACK`.`FLIGHT` (
 `ID` INT NOT NULL AUTO_INCREMENT ,
`FLIGHT_NUM` VARCHAR(15) NOT NULL ,
`ORIGIN` VARCHAR(45) NOT NULL ,
`ARRIVAL_TIME` DATETIME NOT NULL ,
`STATUS` VARCHAR(45) NOT NULL ,
`CAROUSEL_ID` INT NOT NULL ,
`GATE_ID` INT NOT NULL ,
`AIRCRAFT_TYPE_ID` INT NOT NULL ,
PRIMARY KEY (`ID`) ,
INDEX `fk_FLIGHT_CAROUSEL1` (`CAROUSEL_ID` ASC) ,
INDEX `fk_FLIGHT_GATE1` (`GATE_ID` ASC) ,
INDEX `fk_FLIGHT_AIRCRAFT_TYPE1` (`AIRCRAFT_TYPE_ID` ASC) ,
CONSTRAINT `fk_FLIGHT_CAROUSEL1`
FOREIGN KEY (`CAROUSEL_ID` )
REFERENCES `BAG_TRACK`.`CAROUSEL` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_FLIGHT_GATE1`
FOREIGN KEY (`GATE_ID` )
REFERENCES `BAG_TRACK`.`GATE` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_FLIGHT_AIRCRAFT_TYPE1`
FOREIGN KEY (`AIRCRAFT_TYPE_ID` )
REFERENCES `BAG_TRACK`.`AIRCRAFT_TYPE` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = MyISAM
DEFAULT CHARACTER SET = utf8;

工作表

    CREATE  TABLE IF NOT EXISTS `BAG_TRACK`.`JOB` (
  `ID` INT NOT NULL AUTO_INCREMENT ,
  `STATUS` VARCHAR(45) NOT NULL ,
  `LONGITUDE` FLOAT NULL ,
  `LATITUDE` FLOAT NULL ,
  `SPEED` VARCHAR(45) NULL ,
  `JOB_DATE` DATE NULL ,
  `START_TIME` TIME NULL ,
  `TOTAL_TIME` DOUBLE NULL ,
  `COMPLETION_DATE` DATE NULL ,
  `USER_ID` INT NOT NULL ,
  `ROUTE_ID` INT NOT NULL ,
  `FLIGHT_ID` INT NOT NULL ,
  PRIMARY KEY (`ID`, `FLIGHT_ID`) ,
  INDEX `fk_JOB_USER1` (`USER_ID` ASC) ,
  INDEX `fk_JOB_ROUTE1` (`ROUTE_ID` ASC) ,
  INDEX `fk_JOB_FLIGHT1` (`FLIGHT_ID` ASC) ,
  CONSTRAINT `fk_JOB_USER1`
    FOREIGN KEY (`USER_ID` )
    REFERENCES `BAG_TRACK`.`USER` (`ID` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_JOB_ROUTE1`
    FOREIGN KEY (`ROUTE_ID` )
    REFERENCES `BAG_TRACK`.`ROUTE` (`ID` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_JOB_FLIGHT1`
    FOREIGN KEY (`FLIGHT_ID` )
    REFERENCES `BAG_TRACK`.`FLIGHT` (`ID` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

我正在尝试这个简单的插入

 insert into job (status, USER_ID, ROUTE_ID, FLIGHT_ID) VALUES ('UNLOADING', 1, 1, 2)

并获得上述错误。 我的航班表中的数据主键为1。 我已经检查了我的数据库中的所有错误等等,我很遗憾看到问题是什么我会非常感激任何人都可以提供帮助。

由于

1 个答案:

答案 0 :(得分:2)

您的两个表都必须使用innoDb引擎,因为您可以从official doc读取。

  

两个表都必须是InnoDB表,它们不能是TEMPORARY   表。

对于Flight表,您改为ENGINE = MyISAM