node-postgres icon indicating copy to clipboard operation
node-postgres copied to clipboard

ALTER TYPE for enum syntax error for parameter substitution

Open krishchow opened this issue 6 years ago • 3 comments

Node: v13.11.0 Postgres Server: 9.5.21

Snippet: Schema - CREATE TYPE skilltype AS ENUM ('First', 'Second' ); Node - let typename="Third"; pool.query('ALTER TYPE skilltype ADD VALUE $1;',[typename])

The expected behaviour would be for the typename to be parameterized appropriately, however, the parameterization fails for this query (with error: error: syntax error at or near "$1")

krishchow avatar Jun 28 '20 10:06 krishchow

Duplicate of: #539, #1194, #1240, #1258, #1798, #150 🙈


PostgreSQL doesn't support dynamic enum values, the same way it doesn't support dynamic table names, etc.

If you want everything dynamic to be supported, you would need to use a library that implements its own query formatting, like pg-promise. This library however doesn't do its own query formatting, it only sends variables into the server, to be formatted there, hence the limitations.

vitaly-t avatar Jun 28 '20 18:06 vitaly-t

https://www.npmjs.com/package/pg-format can format literals (with %L) into queries that don’t support parameterization.

charmander avatar Jun 28 '20 18:06 charmander

Duplicate of #539

charmander avatar Jun 28 '20 18:06 charmander