Skip to content

Union support for Postgres #568

Closed
@alextanhongpin

Description

@alextanhongpin

Hi, I am wondering if there will be union support for postgres.

Currently running queries with union support produces error.

Postgres Version: 12.2
Table:

CREATE TABLE subscriber (
    id uuid DEFAULT uuid_generate_v1mc() PRIMARY KEY,
    subscription_id uuid NOT NULL REFERENCES subscription(id),
    user_id uuid NOT NULL REFERENCES user(id),
    created_at timestamp with time zone NOT NULL DEFAULT now(),
    updated_at timestamp with time zone NOT NULL DEFAULT now(),
    deleted_at timestamp with time zone,
    CONSTRAINT subscriber_user_id_subscription_id_key UNIQUE (user_id, subscription_id)
);

Query (naive example, not actual production query):

-- name: FindSubscribers :many
SELECT * FROM subscriber
UNION
SELECT * FROM subscriber;

Output:

error generating code: template: table:134:62: executing "interfaceCode" at <.Ret.Type>: error calling Type: no type for GoQueryValue:

The following works, but creates duplicate fields:

-- name: FindSubscribers :many
SELECT * FROM (
    SELECT * FROM subscriber
    UNION
    SELECT * FROM subscriber
)

Output:

type FindSubscribersRow struct {
	ID               uuid.UUID    `json:"id"`
	SubscriptionID   uuid.UUID    `json:"subscription_id"`
	UserID           uuid.UUID    `json:"user_id"`
	CreatedAt        time.Time    `json:"created_at"`
	UpdatedAt        time.Time    `json:"updated_at"`
	DeletedAt        sql.NullTime `json:"deleted_at"`
	ID_2             uuid.UUID    `json:"id_2"`
	SubscriptionID_2 uuid.UUID    `json:"subscription_id_2"`
	UserID_2         uuid.UUID    `json:"user_id_2"`
	CreatedAt_2      time.Time    `json:"created_at_2"`
	UpdatedAt_2      time.Time    `json:"updated_at_2"`
	DeletedAt_2      sql.NullTime `json:"deleted_at_2"`
}

Many thanks ahead 😄

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