Search code examples
javamysqljdbcsql2o

foreign key always stays null


I need a bit of assistance in getting a connection between my two tables

These are the tables where "idPatient is the foreign key" enter image description here

I fill the table "tanden" like this

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();
    }
}

Everything is added nicely but the idPatient stays null

enter image description here


Solution

  • You should include idPatient in your insert if you want to set value to it. 'foreign key' doesnot mean that it will be set value automically.