porsager / postgres

Postgres.js - The Fastest full featured PostgreSQL client for Node.js, Deno, Bun and CloudFlare
The Unlicense
7.09k stars 259 forks source link

Columns don't expand properly in INSERT SELECT statements #769

Open baileywickham opened 6 months ago

baileywickham commented 6 months ago

In this case, ${uniqueColumns} should be a list of columns to insert data from the select statement but the generated sql statement uses the values syntax.

Typescript:

const uniqueColumns = ['uniqueId']
await sql`INSERT INTO "schema".${stagingRelation} ${uniqueColumns}
        SELECT ${uniqueColumns} FROM "schema".${dataRelation} 
        WHERE col IN ${sql(col)} ON CONFLICT DO NOTHING`;

Generated SQL statement:

INSERT INTO "schema"."table_staging" ("0","1","2","3","4","5","6","7")values($1,$2,$3,$4,$5,$6,$7,$8) SELECT "uniqueId" FROM "schema"."table" WHERE col IN ($9,$10,$11) ON CONFLICT DO NOTHING

Best I can tell this is caused by this line of code. Let me know if there is anything I can do to help.