-
Notifications
You must be signed in to change notification settings - Fork 286
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
feature request: add on conflict set to dynamic insert query helper #217
Comments
Should by possible by doing this 🙂 const insertAnalysisT = () => sql`
INSERT INTO base_fees_per_block
${ sql(block) }
ON CONFLICT (number) DO UPDATE
SET ${ sql(block) }
` |
unfortunatelly it doesn't work for me
even if i hard code the conflict id, it throws an error what am i doing wrong? |
I'm not sure but I think the issue is with the conflict clause:
you're supposed to provide the name of the column, I believe for column names the syntax would be `${sql(key)}` Maybe. You can try |
i've done it already :( and this was my error
|
You're missing sql() around key 😊 |
i've tested it too... but please read carefully, what i've wrote - even this configuration
throws an error |
Sorry. Replied on mobile, so missed the subsequent replies before writing.
What version are you on?
… On 15 Jun 2022, at 20.01, Anton Brams ***@***.***> wrote:
You're missing sql() around key 😊
i've tested it too... but please read carefully, what i've wrote - even this configuration
sql`
INSERT INTO ${sql(table)} ${sql(rows)}
ON CONFLICT (id) DO UPDATE
SET ${sql(rows)}
`
throws an error "PostgresError: column "0" of relation "orders_status" does not exist"
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you modified the open/close state.
|
no prob <3 |
Ah I see.. you can't supply an array (multiple rows) to sql`
INSERT INTO ${sql(table)} ${ sql(row) }
ON CONFLICT (${ sql(key) }) DO UPDATE
SET ${ sql(row) }
` If you want multiple rows to work and still be completely dynamic around columns you have to use fragments and then you can do it like this by using sql`
INSERT INTO ${sql('test')} ${sql(rows)}
ON CONFLICT (${ sql(key) }) DO UPDATE
SET ${
Object.keys(rows[0]).map((x, i) =>
sql`${ i ? sql`,` : sql``}${ sql(x) } = excluded.${ sql(x) }`
)
}
` |
jesus..... this is such a crazy syntax :D |
Crazy indeed, but it's whats keeping you safe from sql injections 😝 For your sample above it generates the following query: INSERT INTO "test" ("id","title")values($1,$2),($3,$4)
ON CONFLICT ("id") DO UPDATE
SET "id" = excluded."id", "title" = excluded."title" |
thanks! It works! |
Nice, this syntax for Here's another version with more expressive variable names and TypeScript: export async function upsertBenefitsUnauthenticated(benefits: BenefitInput[]) {
return await sql<Benefit[]>`
INSERT INTO benefits
${sql(benefits)}
ON CONFLICT
(id)
DO UPDATE SET
${Object.keys(benefits[0]!).map((field, count) =>
sql`${count > 0 ? sql`,` : sql``} ${sql(field)} = excluded.${sql(field)}`,
)}
RETURNING
*
`;
} |
Oh interesting, this query above is giving me a TypeScript error though, hm... 🤔 Wonder if the types for
![]() |
Apparently it's a problem with the |
I have a query like so:
Because of the conflict but I can't use the dynamic query helper. A helper that simply sets all columns that are inserted on a conflict with a specifiable list of columns (in this case just
number
but could be multiple), would be quite flexible and for me helpful. This query is a lot longer in actuality.If there are others (👍 ) it may be interesting to implement!
The text was updated successfully, but these errors were encountered: