-
Notifications
You must be signed in to change notification settings - Fork 295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merge and casting in dynamic queries #584
Comments
I tried this
but that errors out at the comma before 'birthdate' This below works, but has an issue
since this translates to empty fields
Which is close! But I am guessing something in postgres has to translate this correctly. |
I haven't used MERGE much yet, mostly because it doesn't support returning the modified rows :-( Could you maybe use on conflict? Even so. Is this an issue with this library or just the query? |
@porsager I could use On Conflict, but I was reading there would be better performance in Merge for Postgres and also chance for data corruption with On Conflict, especially with several operations. And really, to me makes the statement more readable and standard SQL language. This is what the fine internets told me:
|
hello @half-metal and @porsager i have faced the same issue today (more than one year later 😃)... looks there some kind of issue/feature in the query builder when using a object and specifying some keys, so i had to transform the array of objects to an array of arrays before using it inside the query. i do not know if you already found a fix... but for me, the following code works: const data = [{id: '1', name: '2', color: '3'},{id: '4', name: '5', color: '6'}];
const updated_at_or_created_at = (new Date()).toISOString();
const insert_values = data.map((c) => [c.id, c.name, c.color]);
await sql_transaction`
MERGE INTO public.your_table
USING (VALUES ${
sql_transaction(insert_values)
})
AS input (id, name, color)
ON input.id = public.your_table.id
WHEN MATCHED
THEN UPDATE SET
name = input.name,
color = input.color,
updated_at = ${updated_at_or_created_at}
WHEN NOT MATCHED
THEN
INSERT (
created_at,
id,
name,
color
) VALUES(
${updated_at_or_created_at},
input.id,
input.name,
input.color
)
`; |
I was attempting to verify I could do a merge query with 'postgres'
and tried several variations, and came across another issue, I can't merely cast birthdate like this
s (baby_id, birthdate::date, name)
.Debug shows something like this
So I guess two issues
The text was updated successfully, but these errors were encountered: