postgres icon indicating copy to clipboard operation
postgres copied to clipboard

`insert into table as alias` not work with dynamic inserts

Open panmenghan opened this issue 2 years ago • 1 comments

Hello.

This is the reproduction code:

const postgres = require('postgres')
const sql = postgres()

main()

async function main() {
  await sql`CREATE TABLE IF NOT EXISTS my_users (id serial, name text)`

  await sql`INSERT INTO my_users AS u (name) VALUES('name')` // work, with alias

  await sql`INSERT INTO my_users ${sql({name: 'name'})}` // work, with dynamic inserts

  await sql`INSERT INTO my_users AS u ${sql({name: 'name'})}` // not work, with both alias and dynamic inserts
  // error: PostgresError: syntax error at or near "$1"
}

version information:

PostgreSQL: 15
postgres: 3.3.1

panmenghan avatar Oct 28 '22 15:10 panmenghan

Thanks a lot. I think that's gonna be one for v4 when adding more explicit helper methods.

porsager avatar Jun 26 '23 00:06 porsager