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

Postgres sqlc query generation fails for returning properties in the USING table during a DELETE #1714

Closed
buildbreaker opened this issue Jun 28, 2022 · 2 comments

Comments

@buildbreaker
Copy link

buildbreaker commented Jun 28, 2022

Version

1.14.0

What happened?

I'm trying to write a DELETE query which I want to join the results with another table. The way that I am familiar with is to use the USING keyword but that seems to be scoped to the WHERE clause only. When I try returning the columns from the USING table, sqlc generate complains that it cant find the column in the RETURNING block.

I have an example with CI testing/output here.
Add sqlc DELETE/USING issue demonstration contains the commit for the "working path".
failure case introduces the problematic query. The CI error can be found here.

I can get around this specific issue by using CTEs but it'd be nice to get this way working also!

Relevant log output

Error message from the "Failing query" in the SQL queries section:

    # package demo
    query.sql:26:15: column "b_id" does not exist

Database schema

CREATE TABLE a
(
    a_id TEXT,
    b_id_fk TEXT
);
CREATE TABLE b
(
    b_id TEXT
);

SQL queries

Query that works:

-- name: GetSomeDeletedOk :many
DELETE FROM a
    USING b
WHERE a.b_id_fk = b.b_id
    RETURNING *; -- note no columns from b in model

Failing query:

-- name: GetSomeDeletedNotOk :many
DELETE FROM a
USING b
WHERE a.b_id_fk = b.b_id
RETURNING b.b_id; -- column "b_id" does not exist

Configuration

version: 1
packages:
  - path: "demo"
    name: "demo"
    engine: "postgresql"
    schema: "schema.sql"
    queries: "query.sql"

Playground URL

https://play.sqlc.dev/p/1ca4c468637ee1d35988be307ea517680fce1fb63af2b816a5a9af4361bf4f68

What operating system are you using?

macOS

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

@buildbreaker buildbreaker added bug Something isn't working triage New issues that hasn't been reviewed labels Jun 28, 2022
@kyleconroy kyleconroy removed the triage New issues that hasn't been reviewed label Aug 29, 2022
@saquibmian
Copy link

I've been hit by this as well. Are there any workarounds for this?

kyleconroy added a commit that referenced this issue Oct 16, 2023
kyleconroy added a commit that referenced this issue Oct 18, 2023
* fix(compiler): Pull in array information from analyzer
Fixes #1532
* test(analyzer): Add testcase for #1574
* test: Added test for #1634
* test: Add test case for #1646
* test: Add test for #1714
* Fixes #1912
* test: Add case for #1916
* test: Add two test cases
#1917
#1545
* test: Add case for #1979
* test: Add case for #1990
@kyleconroy
Copy link
Collaborator

This is fixed in v1.23.0 by enabling the database-backed query analyzer. We added a test case for this issue so it won’t break in the future.

You can play around with the working example on the playground

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