Java 外键始终保持为空
我需要一些帮助来连接我的两张桌子 这些表格中的“idPatient是外键” 我像这样填写表格“tanden”Java 外键始终保持为空,java,mysql,jdbc,sql2o,Java,Mysql,Jdbc,Sql2o,我需要一些帮助来连接我的两张桌子 这些表格中的“idPatient是外键” 我像这样填写表格“tanden” public void addTandenToDatabase(int id, int fdi, String voorstelling, String toestand) { String insertSql = "insert into tanden(id, fdi, voorstelling, toestand) values (:idVal, :fdiVal, :voo
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表中选取来将idPatient列值插入到tanden表中。您必须通过从patienten表中选取来将idPatient列值插入到tanden表中。如果您想设置值,您应该将
idPatient
包含在插入
中“外键”并不意味着它将自动设置值。如果要设置值,您应该在插入中包含idPatient
。”“外键”并不意味着它将自动设置为值。您在tanden表中的id列应设置为主键和自动递增,并且您必须在insert中设置idPatient
insert into tanden(idPatient, fdi, voorstelling, toestand) values(:idVal,:fdiVal, :voorstellingVal, :toestandVal)";
(您在子表中设置的idPatient必须存在于父表中您在tanden表中的id列应设置为主键并自动递增,并且您必须在insert中设置idPatient
insert into tanden(idPatient, fdi, voorstelling, toestand) values(:idVal,:fdiVal, :voorstellingVal, :toestandVal)";
(您在子表中设置的idPatient必须存在于父表中idPatient不包括在插入查询的值中。您没有指定它。您希望以某种方式自动检索它吗?是的,但我如何指定它您至少需要插入一个Patient。然后,知道它的id,将它用作idPati的值tanden中的ent列。idPatient不包括在INSERT查询的值中。您没有指定它。您希望以某种方式自动检索它吗?是的,但我如何指定它?您至少需要插入一个patienten。然后,知道它的id,将它用作tanden中idPatient列的值。如果我尝试在外文中插入某个内容我得到的密钥:线程“AWT-EventQueue-0”org.sql2o.Sql2oException中的异常:executeUpdate中出错,无法添加或更新子行:外键约束失败(ToOnDesselaere
tanden
,约束id
外键(id
)引用patienten
(idPatient
)如果我试图在外键中插入某个内容,我会得到以下结果:线程“AWT-EventQueue-0”org.sql2o.Sql2oException中的异常:executeUpdate中出错,无法添加或更新子行:外键约束失败(ToOnDesselaere
tanden
,约束id
外键(id
)参考patienten
(idPatient
)关于删除无操作关于更新无操作)