postgres icon indicating copy to clipboard operation
postgres copied to clipboard

Columns don't expand properly in INSERT SELECT statements

Open baileywickham opened this issue 1 year ago • 0 comments

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.

baileywickham avatar Jan 05 '24 17:01 baileywickham