You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sorry if this is a basic question, but I am new to both postgres and node.
I have the following postgres table defined:
CREATE TABLE events (
id serial PRIMARY KEY NOT NULL,
externalid varchar(256) NOT NULL,
sport varchar(256) NOT NULL,
title varchar(256) NOT NULL,
startdate timestamp without time zone NOT NULL,
home varchar(256) NOT NULL,
away varchar(256) NOT NULL
)
A simple insert like this works fine: await sql`insert into events ${sql(events)}`;
But ideally I would like to perform a merge query that will insert any items that exist in the array but not in the database, and update any existing records if there is a match with an item in the array.
This is my query currently:
await sql`
MERGE INTO events AS target
USING (VALUES ${sql(events)})
AS source(externalid, sport, title, startdate, home, away)
ON target.externalid = source.externalid
WHEN MATCHED THEN
UPDATE SET title = source.title
WHEN NOT MATCHED THEN
INSERT (externalid, sport, title, startdate, home, away)
VALUES (source.externalid, source.sport, source.title, source.startdate, source.home, source.away);
`;
but this gives me the following error: column "startdate" is of type timestamp without time zone but expression is of type text
However, if I use a specific array item like so, the merge does work:
const test = events[0];
test.title = "test123";
await sql`
MERGE INTO events AS target
USING (VALUES ${sql(events)})
AS source(externalid, sport, title, startdate, home, away)
ON target.externalid = source.externalid
WHEN MATCHED THEN
UPDATE SET title = source.title
WHEN NOT MATCHED THEN
INSERT (externalid, sport, title, startdate, home, away)
VALUES (${test.externalid}, ${test.sport}, ${test.title}, ${test.startdate}, ${test.home}, ${test.away});
`;
Given that the first and third examples I gave work, I'm inclined to think that the issue is not with the type as I have it in my application nor with the syntax of the merge, but I could be wrong. I have tried using startdate::timestamp in the VALUES clause of the merge and changing the type of startdate to be a string, along with a few other attempts, but nothing seems to work. Any help would be greatly appreciated.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Sorry if this is a basic question, but I am new to both postgres and node.
I have the following postgres table defined:
and an array of the following type:
A simple insert like this works fine:
await sql`insert into events ${sql(events)}`;
But ideally I would like to perform a merge query that will insert any items that exist in the array but not in the database, and update any existing records if there is a match with an item in the array.
This is my query currently:
but this gives me the following error:
column "startdate" is of type timestamp without time zone but expression is of type text
However, if I use a specific array item like so, the merge does work:
Given that the first and third examples I gave work, I'm inclined to think that the issue is not with the type as I have it in my application nor with the syntax of the merge, but I could be wrong. I have tried using
startdate::timestamp
in theVALUES
clause of the merge and changing the type of startdate to be astring
, along with a few other attempts, but nothing seems to work. Any help would be greatly appreciated.Beta Was this translation helpful? Give feedback.
All reactions