Skip to content

sqlc.embed handle CTE #3128

Open
Open

Description

@alexandre-durand

What do you want to change?

sqlc.embed does not recognize type from CTE (see repo link)

I have 2 examples:

  • one that works correctly :
WITH filtered_customers AS (
    SELECT c.*
    FROM customers AS  c
    WHERE c.deleted_at IS NULL
    AND (array_upper(@customer_ids::uuid[], 1) IS NULL OR c.id = ANY(@customer_ids))
    AND sqlc.narg('is_active')::bool IS NULL OR is_active = sqlc.narg('is_active')
), customer_managers_agg AS (
    SELECT cm.customer_id, array_agg(cm.user_id)::uuid[] AS managers
    FROM customers_managers AS cm
    INNER JOIN filtered_customers AS c ON c.id = cm.customer_id
    GROUP BY cm.customer_id
) SELECT sqlc.embed(c), cm.managers
FROM filtered_customers AS c
LEFT JOIN customer_managers_agg AS cm ON c.id = cm.customer_id
WHERE (array_upper(@manager_ids::uuid[], 1) IS NULL OR cm.managers && @manager_ids);

it generates:

type ListCustomersRow struct {
	Customer Customer
	Managers []uuid.UUID
}
  • one that fails:
-- name: CreateCustomer :one
WITH inserted_customer AS (
    INSERT INTO customers (
        name,
        team_id
    ) VALUES (
        @name,
        @team_id
    )
    RETURNING *
), inserted_managers AS (
    INSERT INTO customers_managers (customer_id, user_id)
    SELECT c.id AS customer_id, unnest(@manager_ids::uuid[]) AS user_id
    FROM inserted_customer AS c
)
SELECT sqlc.embed(c), @manager_ids::uuid[] AS managers
FROM inserted_customer AS c;

it generates the following error:
unable to resolve table with "sqlc.embed(c)": relation "c" does not exist

What database engines need to be changed?

No response

What programming language backends need to be changed?

No response

Activity

  1. removed
    triageNew issues that hasn't been reviewed
    on Aug 5, 2024
  2. dbarrosop commented on Nov 11, 2024

    @dbarrosop

    This would be very nice if supported.

  3. zyriab commented on Apr 2, 2025

    @zyriab

    Would love to see this :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions