外键始终保持为空

时间:2016-05-27 08:38:15

标签: java mysql jdbc sql2o

我需要一些帮助来获取我的两个表之间的连接

这些是“idPatient是外键”的表 enter image description here

我像这样填写“tanden”表

public void addTandenToDatabase(int id, int fdi, String voorstelling, String toestand) {
    String insertSql = "insert into tanden(id, fdi, voorstelling, toestand) values (:idVal, :fdiVal, :voorstellingVal, :toestandVal)";
    try (Connection con = sql2o.open()) {
        con.setRollbackOnException(false);
        con.createQuery(insertSql)
                .addParameter("idVal", id)
                .addParameter("fdiVal", fdi)
                .addParameter("voorstellingVal", voorstelling)
                .addParameter("toestandVal", toestand)
                .executeUpdate();
    }
}

一切都很好地添加但idPatient保持为空

enter image description here

3 个答案:

答案 0 :(得分:4)

如果要为其设置值,则应在idPatient中加入insert。 '外键'并不意味着它将自动设定值。

答案 1 :(得分:1)

您必须从patienten表中获取idPatient列值到tanden表中。

答案 2 :(得分:1)

tanden表中的id列应设置为主键和自动增量,并且必须在插入中设置idPatient

insert into tanden(idPatient, fdi, voorstelling, toestand) values(:idVal,:fdiVal, :voorstellingVal, :toestandVal)";

(您在子表中设置的idPatient必须存在于父表