Skip to content

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

Closed

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

Activity

  1. added
    bugSomething isn't working
    triageNew issues that hasn't been reviewed
    on Jun 28, 2022
  2. removed
    triageNew issues that hasn't been reviewed
    on Aug 29, 2022
  3. saquibmian commented on Jan 17, 2023

    @saquibmian

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

  4. kyleconroy commented on Oct 24, 2023

    @kyleconroy
    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

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