This is a moderately complicated CTE that updates a number of tables depending on the content of a JSON argument. When executing this function,
Postgres throws a column reference "explode" is ambiguous error. The line the error is referring to actually has a table name qualifying the column reference.
I'm pretty sure I'm right about which line is causing the problem as if I remove the line I've marked below, the error disappears.
I've been trying to figure this one out some time and I have no idea.
CREATE OR REPLACE FUNCTION prd.update_composition (json, OUT result json) AS
$$
BEGIN
-- Update and return the new composition
WITH composition AS (
UPDATE prd.composition SET (
explode
) = (
payload_composition.explode
)
FROM json_to_record($1) AS payload_composition (
id integer,
explode boolean
)
WHERE id IS NOT NULL
RETURNING *
), payload_component AS (
SELECT
composition.composition_id,
component."productId" AS product_id,
component.quantity,
component.removed
FROM json_to_recordset($1->'components') AS component (
"productId" integer,
quantity numeric(10,3),
removed boolean
)
CROSS JOIN composition
), updated_component AS (
UPDATE prd.component existing SET (
product_id,
quantity
) = (
component.product_id,
component.quantity
)
FROM (
SELECT
composition_id,
product_id,
quantity
FROM payload_component
WHERE removed IS NOT TRUE
) component
WHERE existing.composition_id = component.composition_id
RETURNING *
), deleted AS (
DELETE FROM prd.component existing
USING (
SELECT
composition_id,
product_id
FROM payload_component
WHERE removed IS TRUE
) payload_deleted
WHERE existing.composition_id = payload_deleted.composition_id
AND existing.product_id = payload_deleted.product_id
)
SELECT json_strip_nulls(to_json(r)) INTO result
FROM (
SELECT
composition.composition_id,
composition.explode -- HERE IS THE OFFENDING LINE --
FROM composition
) r;
END
$$
LANGUAGE 'plpgsql' SECURITY DEFINER;
Why is there an ambiguous column reference error with this code?
explodecolumn is not qualified with a table name. Here:UPDATE prd.composition SET ( --> explode <-- ) = ( payload_composition.explode ) FROM json_to_record($1) AS payload_composition ( id integer, explode boolean )It does not know where to get this column from.