porsager / porsager/postgres

Problem executing Common Table Expression (WITH query)

Open
#913 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Hello,

Driver fails to execute WITH query as below:

    const translation = [["en", "My Label"], ["ru", "Моя Метка"]]
    console.log(translation)

    const id = (await sql`
      with t as (
        insert into i18n.text default values returning id
      ) insert into i18n.translation (id, lang, text)
        select id, lang, text from t
          cross join (values ${sql(translation)}) as l(lang, text)
      returning id`)[0].id

    return id

Execution log:

❯ bun run src/index.ts
[
  [ "en", "My Label" ], [ "ru", "Моя Метка" ]
]
212 | function escapeIdentifiers(xs, { transform: { column } }) {
213 |   return xs.map(x => escapeIdentifier(column.to ? column.to(x) : x)).join(',')
214 | }
215 |
216 | export const escapeIdentifier = function escape(str) {
217 |   return '"' + str.replace(/"/g, '""').replace(/\./g, '"."') + '"'
                         ^
TypeError: str.replace is not a function. (In 'str.replace(/"/g, '""')', 'str.replace' is undefined)
      at escape (/Users/andrey/work/datalake/node_modules/postgres/src/types.js:217:20)
      at map (:1:11)
      at escapeIdentifiers (/Users/andrey/work/datalake/node_modules/postgres/src/types.js:220:8)
      at stringify (/Users/andrey/work/datalake/node_modules/postgres/src/types.js:308:16)
      at build (/Users/andrey/work/datalake/node_modules/postgres/src/connection.js:389:16)
      at execute (/Users/andrey/work/datalake/node_modules/postgres/src/connection.js:318:18)
      at ReadyForQuery (/Users/andrey/work/datalake/node_modules/postgres/src/connection.js:881:23)
      at handle (/Users/andrey/work/datalake/node_modules/postgres/src/connection.js:807:5)
      at data (/Users/andrey/work/datalake/node_modules/postgres/src/connection.js:559:28)
      at emit (node:events:180:48)
      at addChunk (node:stream:2029:22)
      at readableAddChunk (node:stream:1983:30)
      at data (node:net:76:9)
      at cachedError (/Users/andrey/work/datalake/node_modules/postgres/src/query.js:170:23)
      at new Query (/Users/andrey/work/datalake/node_modules/postgres/src/query.js:34:10)
      at sql (/Users/andrey/work/datalake/node_modules/postgres/src/index.js:210:8)
      at /Users/andrey/work/datalake/packages/proto/src/api.ts:39:23
      at cachedError (/Users/andrey/work/datalake/node_modules/postgres/src/query.js:170:23)
      at new Query (/Users/andrey/work/datalake/node_modules/postgres/src/query.js:34:10)
      at sql (/Users/andrey/work/datalake/node_modules/postgres/src/index.js:210:8)
      at /Users/andrey/work/datalake/packages/proto/src/api.ts:39:23

Bun v1.1.20 (macOS arm64)

Am I use driver properly? Query works well if executed in cockroachdb console. Also can be bun related, I did not checked with other runtimes.

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 failing reproduction in packages/proto/src/api.ts and the stack-traced code in node_modules/postgres/src/types.js, then run the WITH query under Bun. Compare the driver behavior with the successful CockroachDB console execution and determine what behavior should be corrected so the query no longer fails with the shown TypeError.

Written by the indexing model from the issue text.

Assessment

Tech stack
bun, javascript, postgres, typescript
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.