porsager / porsager/postgres

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

Open
#517 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

v4
Dominant language
JavaScript
Stars
8.7k
Forks
374
Avg merge
11d 16h
Merged PRs (30d)
1

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the JavaScript reproduction using postgres 3.3.1 and PostgreSQL 15, comparing the working aliased insert and dynamic insert with the failing combination. Trace the dynamic insert handling and verify that an INSERT using both a table alias and dynamic values executes without the reported syntax error.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, postgresql
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.