Skip to content

Postgres @named parameters don't always work #605

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

Closed
tv42 opened this issue Jul 21, 2020 · 4 comments
Closed

Postgres @named parameters don't always work #605

tv42 opened this issue Jul 21, 2020 · 4 comments
Labels
📚 postgresql bug Something isn't working

Comments

@tv42
Copy link

tv42 commented Jul 21, 2020

Based on https://github.com/kyleconroy/sqlc/blob/master/docs/named_parameters.md
@foo and sqlc.arg(foo) are identical. However:

-- schema says create table foo ( id bigint );
-- name: demo1 :exec
DELETE FROM foo WHERE id=$1;
-- name: demo2 :exec
DELETE FROM foo WHERE id=@id;
-- name: demo3 :exec
DELETE FROM foo WHERE id=sqlc.arg(id);

results in

$ grep ^func demo.sql.go
func (q *Queries) demo1(ctx context.Context, id sql.NullInt64) error {
func (q *Queries) demo2(ctx context.Context) error {
func (q *Queries) demo3(ctx context.Context, id sql.NullInt64) error {

The @foo form doesn't get recognized in this scenario.

@kyleconroy kyleconroy added bug Something isn't working 📚 postgresql labels Jul 21, 2020
@kyleconroy
Copy link
Collaborator

I'm not sure why, but the @ form works when there's a space between the = and @. Take a look here: https://play.sqlc.dev/p/a2beb1f05746a8cac18a0d161645866c83f95896311b1e1b8ad5943199d7c5dd

Long term, I think I'm going to regret adding the @ form of named parameters because of issues like this.

@maxhawkins
Copy link
Contributor

Sorry if this is digging up something that got settled in #71, but I wish the syntax was :param instead of @param. That way you could use psql to debug your queries against a running database:

psql --variable "param=1" -f queries.sql database

Unsure if using that syntax would help with the parsing problem here.

@kyleconroy
Copy link
Collaborator

@maxhawkins The :param syntax doesn't work with the MySQL parser.

@tv42
Copy link
Author

tv42 commented May 29, 2024

If @foo is not even supposed to work right, https://docs.sqlc.dev/en/latest/howto/named_parameters.html needs to be updated to not recommend it as a shortcut.

If the sqlc.arg() syntax is too verbose for your taste, you can use the @ operator as a shortcut.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📚 postgresql bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants