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

pg: Cannot interpolate values into Postgres 14-style JSON queries #284

Open
ejmartin504 opened this issue Feb 24, 2023 · 0 comments
Open

Comments

@ejmartin504
Copy link

ejmartin504 commented Feb 24, 2023

Example queries in code, where PrivacyMode is an enum with property public, which evaluates to "public"

Attempt 1 (single and double quotes)

import { sql } from '@databases/pg';

return DB.query(sql`
    SELECT * from users
   WHERE
     privacy['profile'] = '"${sql.value(PrivacyMode.public)}"';
`;

error: "could not determine data type of parameter $1"

Attempt 2 (single quotes)

return DB.query(sql`
    SELECT * from users
   WHERE
     privacy['profile'] = '${sql.value(PrivacyMode.public)}'
`;

error: "invalid input syntax for type json"

Attempt 3 (no quotes):

return DB.query(sql`
    SELECT * from users
   WHERE
     privacy['profile'] = ${sql.value(PrivacyMode.public)}
`;

error: "invalid input syntax for type json"

Attempt 4 (no sql.value):

return DB.query(sql`
    SELECT * from users
   WHERE
     privacy['profile'] = '"${PrivacyMode.public)}"'
`;

error: "You do not need to wrap values in 'quotes' when using @databases. Any JavaScript string passed via ${...} syntax is already treated as a string. Please remove the quotes around this value."

The other options, without sql.value, all fail in similar ways.

The only thing that does work is not interpolating the value:

return DB.query(sql`
    SELECT * from users
   WHERE
     privacy['profile'] = '"public"'
`;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant