我有两张桌子。第一个表 Patienten 有一个自动增量 ID。一个“耐心”有多个“gebit”。idPatient 是两个表的外键。
病人:
位:
现在我想填写“gebit”表,但我不断收到错误。
我的代码:
public void addTandenToDatabase(int fdi, String voorstelling, String toestand) {
String insertSql = "insert into gebit(fdi, voorstelling, toestand) values (:fdiVal, :voorstellingVal, :toestandVal)";
try (Connection con = sql2o.open()) {
con.setRollbackOnException(false);
con.createQuery(insertSql)
.addParameter("fdiVal", fdi)
.addParameter("voorstellingVal", voorstelling)
.addParameter("toestandVal", toestand)
.executeUpdate();
}
}
我得到的错误:
我尝试了第二种方法,在 gebit 中向 idPatient 添加一个值:
public void addTandenToDatabase(int fdi, String voorstelling, String toestand) {
String insertSql = "insert into gebit(idPatient, fdi, voorstelling, toestand) values (:idVal, :fdiVal, :voorstellingVal, :toestandVal)";
try (Connection con = sql2o.open()) {
con.setRollbackOnException(false);
con.createQuery(insertSql)
.addParameter("fdiVal", fdi)
.addParameter("idVal", fdi)
.addParameter("voorstellingVal", voorstelling)
.addParameter("toestandVal", toestand)
.executeUpdate();
}
}
但这给了我这个错误:
Error in executeUpdate, Cannot add or update a child row: a foreign key constraint fails (`toondoesselaere`.`gebit`, CONSTRAINT `idPatient` FOREIGN KEY (`idPatient`) REFERENCES `patienten` (`idPatient`) ON DELETE NO ACTION ON UPDATE NO ACTION)