Ошибка:
org.postgresql.util.PSQLException: ERROR: insert or update on table "food_order" violates foreign key constraint "food_order_invoice_id_fkey"
Detail: Key (invoice_id)=(2) is not present in table "invoice".
Сначала я вставляю свои данные в таблицу счетов, а затем вставляю идентификатор этого в качестве ссылки для другого. Я хочу, чтобы эти операторы выполнялись последовательно.
Это мой код:
public static void insertInvoice(Invoice invoice) throws SQLException {
Connection connection = DatabaseConnectionPostgre.connection();
Statement statement = connection.createStatement();
PaymentType paymentType = invoice.getPaymentType();
String query = null;
switch (paymentType){
case Cash:
query = "insert into cash_invoice values(" +
"'" + invoice.getId() + "', " +
"'" + invoice.getDate() + "', " +
"'" + invoice.getTotalPrice() + "', " +
"'" + invoice.getCustomer().getId() + "', " +
"'" + invoice.getInvoiceStatus() + "', " +
"'" + ((CashInvoice) invoice).getDeliveryFee() + "' " +
")";
break;
case Cashless:
query = "insert into cash_invoice values(" +
"'" + invoice.getId() + "', " +
"'" + invoice.getDate() + "', " +
"'" + invoice.getTotalPrice() + "', " +
"'" + invoice.getCustomer().getId() + "', " +
"'" + invoice.getInvoiceStatus() + "', " +
"'" + ((CashlessInvoice) invoice).getPromo() + "' " +
")";
break;
}
statement.executeUpdate(query);
for (Food food:
invoice.getFoods()) {
query = "insert into food_order( food_id, invoice_id) values(" +
"'" + food.getId() + "', " +
"'" + invoice.getId() + "' " +
")";
statement.execute(query);
}
statement.close();
connection.close();
}