Dynamic column selection with nested tagged string error
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 8.7k
- Forks
- 374
- Avg merge
- 11d 16h
- Merged PRs (30d)
- 1
Description
In issue https://github.com/porsager/postgres/issues/588 it says mixing string and tagged string as dynamic columns should work:
const columns = ['name', sql`ST_AsText(location) as loc_txt`];
const cities = await sql`SELECT ${sql(columns)} FROM city`;
However trying this locally, I get an error. As far as I can tell the function escape, str.replace expects only strings, where the tagged string is a Promise.
Is this a regression or intentional?
$ node
Welcome to Node.js v21.2.0.
Type ".help" for more information.
> const sql = require('postgres')();
undefined
> const columns = ['name', sql`ST_AsText(location) as loc_txt`];
undefined
> const cities = await sql`SELECT ${sql(columns)} FROM city`;
Uncaught TypeError: str.replace is not a function
at escape (/home/node_modules/postgres/cjs/src/types.js:217:20)
at /home/node_modules/postgres/cjs/src/types.js:213:22
at Array.map (<anonymous>)
at escapeIdentifiers (/home/node_modules/postgres/cjs/src/types.js:213:13)
at Object.select [as fn] (/home/node_modules/postgres/cjs/src/types.js:139:12)
at Builder.build (/home/node_modules/postgres/cjs/src/types.js:71:17)
at stringifyValue (/home/node_modules/postgres/cjs/src/types.js:109:38)
at stringify (/home/node_modules/postgres/cjs/src/types.js:100:16)
at build (/home/node_modules/postgres/cjs/src/connection.js:223:20)
at execute (/home/node_modules/postgres/cjs/src/connection.js:167:7)
at cachedError (/home/node_modules/postgres/cjs/src/query.js:170:23)
at new Query (/home/node_modules/postgres/cjs/src/query.js:36:24)
at sql (/home/node_modules/postgres/cjs/src/index.js:112:11)
at REPL3:1:52
at cachedError (/home/node_modules/postgres/cjs/src/query.js:170:23)
at new Query (/home/node_modules/postgres/cjs/src/query.js:36:24)
at sql (/home/node_modules/postgres/cjs/src/index.js:112:11)
at REPL3:1:52
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with cjs/src/types.js, especially escapeIdentifiers, escape, and the select builder named in the stack trace. Reproduce the issue with the Node.js example using a string and tagged string in the columns array. Done means the documented dynamic-column example has a defined, tested outcome rather than producing str.replace is not a function.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, nodejs, postgresql
- Domain
- backend, database
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100