Skip to content

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

Closed
@buildbreaker

Description

@buildbreaker

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions