Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

UNION ALL queries generate unexpected row types instead of using the base table type #3832

Open
mgrisole opened this issue Feb 7, 2025 · 0 comments

Comments

@mgrisole
Copy link

mgrisole commented Feb 7, 2025

Description

When using UNION ALL in a query, SQLC generates a new row type (e.g., FindOrCreateAssetsByNameAndSymbolRow) instead of using the base table type (Asset), even though both parts of the UNION are returning the same table structure.

Current Behavior

Given a query like this:

-- name: FindOrCreateAssetsByNameAndSymbol :many
WITH new_assets AS (
  INSERT INTO asset (name, symbol)
  SELECT unnest(@names::text[]), unnest(@symbols::text[])
  WHERE NOT EXISTS (...)
  RETURNING *
)
SELECT * FROM new_assets
UNION ALL
SELECT * FROM asset
WHERE ...;

SQLC generates:

func (q *Queries) FindOrCreateAssetsByNameAndSymbol(ctx context.Context, arg FindOrCreateAssetsByNameAndSymbolParams) ([]FindOrCreateAssetsByNameAndSymbolRow, error)

Expected Behavior

Since both parts of the UNION ALL return the same table structure (asset), the generated function should return the base table type:

func (q *Queries) FindOrCreateAssetsByNameAndSymbol(ctx context.Context, arg FindOrCreateAssetsByNameAndSymbolParams) ([]Asset, error)

What database engines need to be changed?

PostgreSQL

What programming language backends need to be changed?

Go

@mgrisole mgrisole added the enhancement New feature or request label Feb 7, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

1 participant