2016-05-26 3 views
0

私は2つのテーブルを持っています。最初のテーブルpatientenは自動増分IDを持ちます。 1人の "患者"には複数の "gebit"があります。 idPatientは2つのテーブルからの外部キーです。他のテーブルからの自動インクリメントに基づいてデータベースに外部キーを入力します

patienten:

enter image description here

がgebit:

enter image description here

今私は "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(); 
    } 
} 

私が取得エラー:

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

enter image description here

私は私がgebitにidPatientにVALUを追加する第二の方法を試してみましたしかし、それは私にこのエラーを与える:

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) 
+1

gebitにidValという列がありますか? – Unknown

+0

いいえ、列idPatient – Skupaj

答えて

1

あなたはgebit.idPatientreferencespatienten.idPatientとして、foreign keyを破ると、あなたは何のマッチングpatienten.idPatientを持っていないidPatientgebitにレコードを挿入しようとします。

idPatientをご確認のうえ、insertに間違いがないか確認してください。もしそうならば間違った結果のバグを修正してくださいidPatient

+0

どうすれば一致させることができますか? – Skupaj

+0

レコードを挿入するたびに、すべての外部キーが参照するテーブル内の既存の値を参照するようにする必要があります。 –

関連する問題