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

SET value TO json results in PostgresError: syntax error at or near "$1" #640

Closed
csenio opened this issue Jul 15, 2023 · 1 comment
Closed

Comments

@csenio
Copy link

csenio commented Jul 15, 2023

I have a query that looks like this:

	const claims = '{"role":"authenticated","sub":"8609034g-e897-4631-ad07-15d3ebdd7999"}'

	const data = await sql.begin('ISOLATION LEVEL SERIALIZABLE', async (sql) => {
		await sql`SET session role authenticated`
		
		await sql`SET request.jwt.claims TO ${claims}` // <-- this breaks
		
		const changed = await sql`SELECT id, message FROM test`

		return changed
	})

I tried every variation I could think off to insert the sub claim into my query, but it always results in the error PostgresError: syntax error at or near "$1".
I also tried using sql.json which resulted in PostgresError: invalid input syntax for type json
I also tried inlining the json in the request directly and only passing the sub value via interpolation, but that failed as well.

@porsager
Copy link
Owner

porsager commented Jul 15, 2023

Use the set_config and current_setting functions instead, as they support parameters 😉 Be sure to set the is_local value correctly (should most likely be true always)!

https://www.postgresql.org/docs/9.3/functions-admin.html

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

2 participants