外键始终保持为空
foreign key always stays null
我需要一些帮助来连接我的两个 table
这些是 table,其中 "idPatient is the foreign key"
我这样填 table "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 保持为空
您必须通过从 patienten table.
中获取,将 idPatient 列值插入 tanden table
如果要为其设置值,则应在 insert
中包含 idPatient
。 'foreign key'不代表会自动设置值。
您在 tanden table 中的 id 列应设置为主键和自动增量,并且您必须在 insert
中设置 idPatient
insert into tanden(idPatient, fdi, voorstelling, toestand) values(:idVal,:fdiVal, :voorstellingVal, :toestandVal)";
(您在子 table 中设置的 idPatient 已经存在于父 table
我需要一些帮助来连接我的两个 table
这些是 table,其中 "idPatient is the foreign key"
我这样填 table "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 保持为空
您必须通过从 patienten table.
中获取,将 idPatient 列值插入 tanden table如果要为其设置值,则应在 insert
中包含 idPatient
。 'foreign key'不代表会自动设置值。
您在 tanden table 中的 id 列应设置为主键和自动增量,并且您必须在 insert
中设置 idPatientinsert into tanden(idPatient, fdi, voorstelling, toestand) values(:idVal,:fdiVal, :voorstellingVal, :toestandVal)";
(您在子 table 中设置的 idPatient 已经存在于父 table