Skip to content

CTE in Update FROM says relation does not exist when runs fine against postgres #4072

@jay-babu

Description

@jay-babu

Version

1.29.0

What happened?

sqlc generate fails

Relevant log output

pkg/db/cohort_item_copy_from_source.sql:1:1: relation "from_cohort_item" does not exist

Database schema

SQL queries

UPDATE
    cohort_item
SET
    name = sub.name
FROM ( WITH from_cohort_item AS (
        SELECT
            ci.pack_size,
            CASE WHEN LENGTH(cib.barcode) = 12 THEN
                SUBSTRING(cib.barcode, 2, 10)
            WHEN LENGTH(cib.barcode) = 8 THEN
                SUBSTRING(cib.barcode, 2, 6)
            ELSE
                cib.barcode
            END AS barcode,
            ci.name
        FROM
            cohort_item ci
            JOIN cohort_item_barcode cib ON ci.id = cib.cohort_item_id
        WHERE
            ci.cohort_id = @source_cohort_id),
        to_cohort_item AS (
            SELECT
                ci.pack_size,
                CASE WHEN LENGTH(cib.barcode) = 12 THEN
                    SUBSTRING(cib.barcode, 2, 10)
                WHEN LENGTH(cib.barcode) = 8 THEN
                    SUBSTRING(cib.barcode, 2, 6)
                ELSE
                    cib.barcode
                END AS barcode,
                ci.id AS cohort_item_id,
                ci.cohort_id
            FROM
                cohort_item ci
                JOIN cohort_item_barcode cib ON ci.id = cib.cohort_item_id
            WHERE
                ci.cohort_id = @target_cohort_id
)
            SELECT DISTINCT
                tci.cohort_item_id,
                fci.name
            FROM
                from_cohort_item fci
                JOIN to_cohort_item tci ON tci.barcode = fci.barcode
                    AND fci.pack_size = tci.pack_size) AS sub
WHERE
    cohort_item.id = sub.cohort_item_id
    AND cohort_item.name <> sub.name
    AND cohort_item.cohort_id = @target_cohort_id;

Configuration

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

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