Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
muratgozel opened this issue Nov 7, 2021 · 7 comments
Closed

Comments

@muratgozel
Copy link

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: []
}
@muratgozel muratgozel added the bug label Nov 7, 2021
@muratgozel muratgozel changed the title Function Calls With Single Quoted Parameters Doesn't Work Function Calls That Require Single Quoted Parameters Doesn't Work Nov 7, 2021
@nponiros
Copy link
Contributor

nponiros commented Nov 7, 2021

I don't know if this is a bug or not (doesn't look like one to me) but I believe that there is a way to get this to work.

First off if I understand the documentation correctly the column name should be quoted as a string literal so you shouldn't use sql.identifier for it.

For the table name you are using you also don't need sql.identifier. You could try the below:

const tableToken = 'auth_session';
const fieldToken = 'id';
const query = sql`select nextval(pg_get_serial_sequence(${tableToken}, ${fieldToken})) as new_id`

In case you need to quote the table name or use a schema you could try the below:

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

Note the single quotes around ${tableToken}.

@gajus
Copy link
Owner

gajus commented Nov 7, 2021

If the value is safe (e.g. comes from predefined list of values), then you can just use sql.raw – that's what it is created for.

@gajus gajus added duplicate and removed bug labels Nov 7, 2021
@gajus
Copy link
Owner

gajus commented Nov 7, 2021

Duplicate of #150

@gajus gajus marked this as a duplicate of #150 Nov 7, 2021
@gajus gajus closed this as completed Nov 7, 2021
gajus added a commit that referenced this issue Nov 7, 2021
@gajus
Copy link
Owner

gajus commented Nov 7, 2021

🎉 This issue has been resolved in version 24.2.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@gajus gajus added the released label Nov 7, 2021
@gajus
Copy link
Owner

gajus commented Nov 7, 2021

You can now use sql.literalValue.

@muratgozel
Copy link
Author

@nponiros thanks for the suggestions but I already tried them and they weren't working. Now I better understand that this is more of a feature request/missing feature than a bug. 😌

@gajus sql.literalValue saved me. thanks. 😌

@nponiros
Copy link
Contributor

nponiros commented Nov 7, 2021

@muratgozel strange it did work for me or at least I thought it did.. I didn’t get an error but I tested it without nextval only with pg_get_serial_sequence so maybe that was the issue or maybe I just had table/column names that happened to work. In any case now there is a better way to do it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants