开发者

Cannot add or update a child row: a foreign key constraint fails

开发者 https://www.devze.com 2023-03-05 23:37 出处:网络
I have a database with the following schema: The following SQL creates the relevant tables: -- -----------------------------------------------------

I have a database with the following schema:

Cannot add or update a child row: a foreign key constraint fails

The following SQL creates the relevant tables:

-- -----------------------------------------------------
-- Table `ninikske_bioldb`.`CodingRegion`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `ninikske_bioldb`.`CodingRegion` (
  `CodRegID` VARCHAR(45) NOT NULL ,
      `CD_Start` INT NOT NULL ,
  `CD_Stop` INT NOT NULL ,
  `ORFs_ORF_ID` VARCHAR(45) NOT NULL ,
  PRIMARY KEY (`ORFs_ORF_ID`, `CodRegID`) ,
  INDEX `fk_Exons_ORFs1` (`ORFs_ORF_ID` ASC) ,
  CONSTRAINT `fk_Exons_ORFs1`
    FOREIGN KEY (`ORFs_ORF_ID` )
    REFERENCES `ninikske_bioldb`.`ORFs` (`ORF_ID` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `ninikske_bioldb`.`Experiment`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `ninikske_bioldb`.`Experiment` (
  `Probe_GenomicPos` INT NOT NULL ,
  `SampleName` VARCHAR(45) NOT NULL ,
  `Intensities` FLOAT NOT NULL ,
  `ExperimentName` VARCHAR(45) NOT NULL ,
  `ProbeID` INT NOT NULL ,
  PRIMARY KEY (`Probe_GenomicPos`, `SampleName`, `ProbeID`) )
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `ninikske_bioldb`.`CE`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `ninikske_bioldb`.`CE` (
  `OrfId` VARCHAR(45) NOT NULL ,
  `CodRegId` VARCHAR(45) NOT NULL ,
  `GenPos` INT NOT NULL ,
  `ExpSam` VARCHAR(45) NOT NULL ,
  `ProbeId` INT NOT NULL ,
      PRIMARY KEY (`OrfId`, `CodRegId`, `GenPos`, `ExpSam`, `ProbeId`) ,
  INDEX `fk_CodingRegion_has_Experiment_Experiment1` (`GenPos` ASC, `ExpSam` ASC, `ProbeId` ASC) ,
  INDEX `fk_CodingRegion_has_Experiment_CodingRegion1` (`OrfId` ASC, `CodRegId` ASC) ,
  CONSTRAINT `fk_CodingRegion_has_Experiment_CodingRegion1`
    FOREIGN KEY (`OrfId` , `CodRegId` )
    REFERENCES `ninikske_bioldb`.`CodingRegion` (`ORFs_ORF_ID` , `CodRegID` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_CodingRegion_has_Experiment_Experiment1`
    FOREIGN KEY (`GenPos` , `ExpSam` , `ProbeId` )
    REFERENCES `ninikske_bioldb`.`Experiment` (`Probe_GenomicPos` , `SampleName` , `ProbeID` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

When I execute the following INSERT statement via Java:

String query = "INSERT INTO ninikske_bioldb.CE VALUES('" + cs.getORF_ID().getORF_ID() + "','" + cs.getCodRegID().getCodRegID() + "'," + cs.getExp().getProbePos() + ",'" + cs.getExp().getExpName()+ "', " + cs.getExp().getProbeID() + ");";
Statement stmt = con.createStatement();
stmt.executeUpdate(query);

I get the following error:

Cannot add or update a child row: a foreign key constraint fails (`ninikske_bioldb/CE`, CONSTRAINT `fk_CodingRegion_has_Experiment_Experiment1` FOREIGN KEY (`GenPos`, `ExpSam`, `ProbeId`) REFERENCES `Experiment` (`Probe_GenomicPos`, `SampleName`, `ProbeID`) )

I read several other questions about th开发者_StackOverflowe same error message, but i can't seem to find the insight I need.

Edit The query is: INSERT INTO ninikske_bioldb.CE VALUES('AT3G01190.1','cd474',67262,'H20', 1709);

When I check I the parent tables, I get following results:

mysql> SELECT * FROM CodingRegion WHERE ORFs_ORF_ID = 'AT3G01190.1';
+----------+----------+---------+-------------+
| CodRegID | CD_Start | CD_Stop | ORFs_ORF_ID |
+----------+----------+---------+-------------+
| cd474    |    67243 |   67649 | AT3G01190.1 | 
| cd475    |    67733 |   67892 | AT3G01190.1 | 
| cd476    |    67991 |   68176 | AT3G01190.1 | 
| cd477    |    68272 |   68484 | AT3G01190.1 | 
+----------+----------+---------+-------------+
4 rows in set (0.00 sec)

mysql> SELECT * FROM CodingRegion WHERE CodRegID = 'cd474';     
+----------+----------+---------+-------------+
| CodRegID | CD_Start | CD_Stop | ORFs_ORF_ID |
+----------+----------+---------+-------------+
| cd474    |    67243 |   67649 | AT3G01190.1 | 
+----------+----------+---------+-------------+
1 row in set (0.03 sec)

mysql> SELECT * FROM Experiment WHERE Probe_GenomicPos = '67262';
+------------------+--------------+-------------+----------------+---------+
| Probe_GenomicPos | SampleName   | Intensities | ExperimentName | ProbeID |
+------------------+--------------+-------------+----------------+---------+
|            67262 | 1signalpart1 |     8.94432 | H20            |    1709 | 
+------------------+--------------+-------------+----------------+---------+
1 row in set (0.00 sec)

mysql> SELECT * FROM Experiment WHERE SampleName = 'H20'
Empty set (0.00 sec)


mysql> SELECT * FROM Experiment WHERE ProbeID = '1709';           
+------------------+--------------+-------------+----------------+---------+
| Probe_GenomicPos | SampleName   | Intensities | ExperimentName | ProbeID |
+------------------+--------------+-------------+----------------+---------+
|            67262 | 1signalpart1 |     8.94432 | H20            |    1709 | 
+------------------+--------------+-------------+----------------+---------+
1 row in set (0.00 sec)

Thanks in advance.


The constraint that's failing, "fk_CodingRegion_has_Experiment_Experiment1", requires that the values you enter for these three columns--GenPos, ExpSam, and ProbeId--already exist in the table "ninikske_bioldb.Experiment" in the columns Probe_GenomicPos, SampleName, and ProbeID. (In that order.)

Look in the table ninikske_bioldb.Experiment for those three values.

SELECT * 
FROM ninikske_bioldb.Experiment
WHERE Probe_GenomicPos = ?
  AND SampleName = '?'
  AND ProbeID = ?

It's possible that

  • the primary key of the table Experiment is wrong (should include ExperimentName rather than SampleName), or
  • the foreign key reference is wrong (should reference ExperimentName rather than SampleName), or
  • the function cs.getExp().getExpName() is the wrong function to use.

I think the last one is the most likely. Probably should be getSampleName(), or something like that.

0

精彩评论

暂无评论...
验证码 换一张
取 消