porsager / porsager/postgres

Value interpolation fails on CREATE ROLE

Open
#1,060 1 comment 1 reaction 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

Code:

import postgres from "postgres";

const sql = postgres(process.env.DS_DB ?? "", {
    debug: true,
});

const test = async () => {
    const roleName = "test_role";
    const password = "test_password";

    const describeResult = await sql`CREATE ROLE ${sql(
        roleName
    )} WITH LOGIN PASSWORD ${password}`.describe();

    console.log("zz", describeResult);
};

test();

This produces error:

node:internal/process/promises:289
            triggerUncaughtException(err, true /* fromPromise */);
            ^

PostgresError: syntax error at or near "$1"
    at ErrorResponse (/<redacted>/node_modules/.pnpm/postgres@3.4.4/node_modules/postgres/cjs/src/connection.js:788:26)
    at handle (/<redacted>/node_modules/.pnpm/postgres@3.4.4/node_modules/postgres/cjs/src/connection.js:474:6)
    at Socket.data (/<redacted>/node_modules/.pnpm/postgres@3.4.4/node_modules/postgres/cjs/src/connection.js:315:9)
    at Socket.emit (node:events:514:28)
    at addChunk (node:internal/streams/readarble:376:12)
    at readableAddChunk (node:internal/streams/readable:349:9)
    at Readable.push (node:internal/streams/readable:286:10)
    at TCP.onStreamRead (node:internal/stream_base_commons:190:23)
    at new Query (/<redacted>/node_modules/.pnpm/postgres@3.4.4/node_modules/postgres/cjs/src/query.js:35:9)
    at sql (/<redacted>/node_modules/.pnpm/postgres@3.4.4/node_modules/postgres/cjs/src/index.js:112:11)
    at test (/<redacted>/lib/dbtest.ts:11:35)
    at /<redacted>/lib/dbtest.ts:18:1
    at evalModule (/<redacted>/node_modules/.pnpm/jiti@1.21.6/node_modules/jiti/dist/jiti.js:1:247313)
    at jiti (/<redacted>/node_modules/.pnpm/jiti@1.21.6/node_modules/jiti/dist/jiti.js:1:245241)
    at Object.<anonymous> (/<redacted>/node_modules/.pnpm/jiti@1.21.6/node_modules/jiti/bin/jiti.js:16:1)
    at Module._compile (node:internal/modules/cjs/loader:1241:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1295:10)
    at Module.load (node:internal/modules/cjs/loader:1091:32)
    at Module._load (node:internal/modules/cjs/loader:938:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12)
    at node:internal/main/run_main_module:23:47 {
  severity_local: 'ERROR',
  severity: 'ERROR',
  code: '42601',
  position: '45',
  file: 'scan.l',
  line: '1188',
  routine: 'scanner_yyerror',
  query: 'CREATE ROLE "test_role" WITH LOGIN PASSWORD $1',
  parameters: [ 'test_password' ],
  args: [ Identifier { value: '"test_role"' }, 'test_password' ],
  types: [ 0 ]
}

But, if I do it like this:

await sql`CREATE ROLE ${sql(roleName)} WITH LOGIN PASSWORD ${sql.unsafe(`'${password}'`)}`;

then it works and produces valid query ..... LOGIN PASSWORD 'test_password'

Am I doing something wrong?

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 failing CREATE ROLE example from lib/dbtest.ts and compare the generated query and parameters with the working sql.unsafe example. Trace the query-building entry point shown in the stack trace to determine whether this interpolation case is expected to work, then add or update regression coverage so the intended behavior is clear.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nodejs, postgresql, 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.