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

Generating FOR UPDATE OF query with postgres schemas fails #3672

Open
GeertJohan opened this issue Oct 24, 2024 · 0 comments
Open

Generating FOR UPDATE OF query with postgres schemas fails #3672

GeertJohan opened this issue Oct 24, 2024 · 0 comments
Labels

Comments

@GeertJohan
Copy link

GeertJohan commented Oct 24, 2024

Version

Tested with sqlc v1.25, v1.26, v1.27

What happened?

When using sqlc with postgres the FOR UPDATE OF statement cannot be used in combination with schemas.

In postgres, this is a valid query:

SELECT *
FROM myschema.mytable
FOR UPDATE OF mytable;

However, sqlc complains that relation "mytable" does not exist.

There are two workarounds. The first is to let sqlc generate invalid SQL, and fix it afterwards.

SELECT *
FROM myschema.mytable
FOR UPDATE OF myschema.mytable;

This query will let sqlc generate Go code. But when running the code, preparing the query will fail because postgres doesn't accept FOR UPDATE OF myschema.mytable. So you'll need to run sed 's/FOR UPDATE OF myschema.mytable/FOR UPDATE OF mytable/g' path/to/queries.sql.go to fix the generate go code. ((This workaround could actually be considered to abuse a secondary bug; as it should not be possible to generate FOR UPDATE OF schema.table as a postgres query.))

The second workaround is to explicitly alias the table in the query;

SELECT *
FROM myschema.mytable AS mytable
FOR UPDATE OF mytable;

Playground URL

https://play.sqlc.dev/p/6dedaab03491761e210fc65f3be208c3e4a60c8de48052495a2dacca08a54b4b

What operating system are you using?

Linux

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

@GeertJohan GeertJohan added the bug Something isn't working label Oct 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant