porsager / porsager/postgres

[BUG] `str.replace is not a function` with 2 `values` CTEs

Open
#1,149 0 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

I have the following (much simplified) code:

const properties = { latitude: '3.1', longitude: '15.6' };
return await sql`
    WITH
        coords(lat, lon) AS (VALUES
            (${properties.latitude}, ${properties.longitude})
        )
        ,props(name) as (VALUES ${sql(Object.keys(properties).map(key => [key]))})
    SELECT '123'
`;

It fails with the following error:

TypeError: str.replace is not a function
    at escape (file:///node_modules/.pnpm/postgres@3.4.8/node_modules/postgres/src/types.js:217:20)
    at file:///node_modules/.pnpm/postgres@3.4.8/node_modules/postgres/src/types.js:213:22
    at Array.map (<anonymous>)
    at escapeIdentifiers (file:///node_modules/.pnpm/postgres@3.4.8/node_modules/postgres/src/types.js:213:13)
    at Object.select [as fn] (file:///node_modules/.pnpm/postgres@3.4.8/node_modules/postgres/src/types.js:139:12)
    at Builder.build (file:///node_modules/.pnpm/postgres@3.4.8/node_modules/postgres/src/types.js:71:17)
    at stringifyValue (file:///node_modules/.pnpm/postgres@3.4.8/node_modules/postgres/src/types.js:109:38)
    at stringify (file:///node_modules/.pnpm/postgres@3.4.8/node_modules/postgres/src/types.js:100:16)
    at build (file:///node_modules/.pnpm/postgres@3.4.8/node_modules/postgres/src/connection.js:228:20)
    at execute (file:///node_modules/.pnpm/postgres@3.4.8/node_modules/postgres/src/connection.js:172:7)
    at cachedError (file:///node_modules/.pnpm/postgres@3.4.8/node_modules/postgres/src/query.js:170:23)
    at new Query (file:///node_modules/.pnpm/postgres@3.4.8/node_modules/postgres/src/query.js:36:24)
    at sql (file:///node_modules/.pnpm/postgres@3.4.8/node_modules/postgres/src/index.js:112:11)

If I remove either one of the CTEs, it works fine. And with both CTEs it works fine when run in a Postgres client.
My scenario resembles that of #913, except I don't have any INSERT in my CTEs.

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

Reproduce the supplied TypeScript query with both CTEs, then trace the failure through postgres/src/types.js, especially escapeIdentifiers and the select builder shown in the stack trace. Done means the query executes with both VALUES CTEs without the str.replace TypeError.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.