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

COALESCE doesn't appear to work within a CTE #3381

Open
Bronya-Rand opened this issue May 14, 2024 · 0 comments
Open

COALESCE doesn't appear to work within a CTE #3381

Bronya-Rand opened this issue May 14, 2024 · 0 comments
Labels
bug Something isn't working triage New issues that hasn't been reviewed

Comments

@Bronya-Rand
Copy link

Bronya-Rand commented May 14, 2024

Version

1.26.0

What happened?

When creating WITH statements in SQL, I've noticed that if I was to use COALESCE within a WITH that updates data, that it would result in sqlc generate saying that the column does not exist even though it does exist within the table using psql.

Relevant log output

# package queries
sql/collections.sql:30:9: column "updated_at" does not exist

Database schema

id | user_id | name | description | created_at | updated_at

SQL queries

-- name: CollectionUpdate :one
WITH updated_collection AS (
    UPDATE user_collections
    SET
        collection_name = COALESCE(sqlc.narg(collection_name), collection_name),
        collection_description = COALESCE(sqlc.narg(collection_description), collection_description),
        is_public = COALESCE(sqlc.narg(is_public), is_public),
        updated_at = COALESCE(sqlc.narg(updated_at), CURRENT_TIMESTAMP)
    WHERE id = sqlc.arg(id)
    RETURNING id
), deleted_characters AS (
    DELETE FROM collection_characters
    WHERE collection_id = (SELECT id FROM updated_collection) AND character_id != ALL(sqlc.narg('character_ids')::uuid[])
    RETURNING *
)

INSERT INTO collection_characters (character_id, collection_id)
SELECT UNNEST(sqlc.narg('character_ids')::uuid[]), (SELECT id FROM updated_collection)
ON CONFLICT DO NOTHING
RETURNING *;

Configuration

version: "2"
sql:
  - schema: "./migrate/migrations/"
    queries: "./sql/"
    engine: "postgresql"
    gen:
      go:
        emit_interface: true
        package: "queries"
        out: "queries"
overrides:
  go: null
plugins: []
rules: []

Playground URL

No response

What operating system are you using?

Linux

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

@Bronya-Rand Bronya-Rand added bug Something isn't working triage New issues that hasn't been reviewed labels May 14, 2024
@Bronya-Rand Bronya-Rand changed the title sqlc.narg appears to not work within a CTE COALESCE doesn'appears to not work within a CTE May 14, 2024
@Bronya-Rand Bronya-Rand changed the title COALESCE doesn'appears to not work within a CTE COALESCE doesn't appear to work within a CTE May 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working triage New issues that hasn't been reviewed
Projects
None yet
Development

No branches or pull requests

1 participant