Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Function Calls That Require Single Quoted Parameters Doesn't Work #309

@muratgozel

Description

@muratgozel

The postgres internal function pg_get_serial_sequence(table_name, col_name) is a bit sensitive with quoting. Both table_name and col_name should be sent single-quoted. The following query works when executed from the psql:

select nextval( pg_get_serial_sequence('auth_session', 'id') ) as new_id;
# returns new_id = [number]

There is no way to achieve this in slonik.

Case:

const tableToken = sql.identifier(['auth_session'])
const fieldToken = sql.identifier(['id'])
const query = sql`select nextval(pg_get_serial_sequence(${tableToken}, ${fieldToken})) as new_id`
const result = await this.pgpool.query(query)

Expected Behavior

The result should return {rows: [{new_id: 1}]}

Current Behavior

It raises an error:

error: column "auth_session" does not exist
  at Parser.parseErrorMessage (/app/node_modules/pg-protocol/dist/parser.js:287:98)
  at Parser.handlePacket (/app/node_modules/pg-protocol/dist/parser.js:126:29)
  at Parser.parse (/app/node_modules/pg-protocol/dist/parser.js:39:38)
  at Socket.<anonymous> (/app/node_modules/pg-protocol/dist/index.js:11:42)
  at Socket.emit (node:events:390:28)
  at addChunk (node:internal/streams/readable:324:12)
  at readableAddChunk (node:internal/streams/readable:297:9)
  at Socket.Readable.push (node:internal/streams/readable:234:10)
  at TCP.onStreamRead (node:internal/stream_base_commons:199:23)

The query object:

query: {
  sql: 'select nextval(pg_get_serial_sequence("auth_session", "id")) as new_id',
  type: 'SLONIK_TOKEN_SQL',
  values: []
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions