I want to insert multiple values in a Postgres table (value, guest_name, guest_room_number, created, employee_id, guest_group_id, table_numbers, show) if multiple values (value, guestName, guestRoomNumber) do not already exist. How can I achieve that?
Thanks for the help.
My try:
await this.db.query(
`INSERT INTO app_trace(value, guest_name, guest_room_number, created, employee_id, guest_group_id, table_numbers, show)
SELECT $1, $2, $3, $4, $5, $6, $7, $8
WHERE NOT EXISTS (SELECT value FROM app_trace WHERE value=$1 AND guest_name=$2 AND guest_room_number=$3)`,
[value, guestName, guestRoomNumber, created, employeeId, guestGroupId, tableNumbers, show]);`
The try is still saving the data, even though the values do exist in the DB.
Example Data in the DB:
INSERT INTO "public"."app_trace"("id","value","guest_name","guest_room_number","created","employee_id","guest_group_id","table_numbers","show")
VALUES
(88,E'test',E'Maierei',E'123',E'2019-08-05 15:15:45.984+00',1,65866,E'90',NULL);
This try still inserts the data, but I do not want that.
await this.db.query(
`INSERT INTO app_trace(value, guest_name, guest_room_number, created, employee_id, guest_group_id, table_numbers, show)
SELECT $1, $2, $3, $4, $5, $6, $7, $8
WHERE NOT EXISTS (SELECT value FROM app_trace WHERE value=$1 AND guest_name=$2 AND guest_room_number=$3)`,
['test', 'Maierei', '123', '2019-08-06 15:15:45.984+00', 1, 123445, '23', true]);`
I want that the statement is not inserting the data, because the values (value, guestName, guestRoomNumber) already exist in the table.
(value, guest_name, guest_room_number)?(value, guest_name, guest_room_number). Should I add one? Is there a possibility to avoid that?