0

私は2つのテーブルを持っています。最初のテーブルpatientenには、自動インクリメントIDがあります。1 つの「患者」には複数の「gebit」があります。idPatient は、2 つのテーブルからの外部キーです。

患者:

ここに画像の説明を入力

ゲビット:

ここに画像の説明を入力

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

私が得るエラー:

ここに画像の説明を入力

gebit で idPatient に値を追加する 2 番目の方法を試しました。

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

しかし、それは私にこのエラーを与えます:

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)
4

1 に答える 1

1

foreign keyaを壊し、一致するを持たない にgebit.idPatient references patienten.idPatientレコードを挿入しようとしました。gebitidPatientpatienten.idPatient

必要なものを確認して、それが間違っているかどうかをidPatient確認する必要があります。insertもしそうなら、間違ったバグを修正してidPatientください。

于 2016-05-26T18:20:38.930 に答える