2

I am trying to execute a dynamic query in a trigger function but I keep getting the Missing FROM-clause entry for table "new"

Why does the error occur and how can I fix it?

CREATE OR REPLACE FUNCTION "Site"."UpdateAncestorModified"()
  RETURNS trigger AS
$BODY$BEGIN
    EXECUTE
        format
        ('
            UPDATE 
                "' || TG_TABLE_SCHEMA || '"."' || TG_TABLE_NAME || '" 
            SET
                modified = NEW.modified
            WHERE
                id = NEW."ancestorId"
            AND
                modified <> NEW.modified
        ')
    USING
        NEW;
    RETURN NEW;
END$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

1 Answer 1

4

I had to change the NEW to the ($1) since it is the reference to the first bound parameter.

The correct query is:

CREATE OR REPLACE FUNCTION "Site"."UpdateAncestorModified"()
  RETURNS trigger AS
$BODY$BEGIN
    EXECUTE
        format
        ('
            UPDATE 
                "' || TG_TABLE_SCHEMA || '"."' || TG_TABLE_NAME || '" 
            SET
                modified = ($1).modified
            WHERE
                id = ($1)."ancestorId"
            AND
                modified <> ($1).modified
        ')
    USING
        NEW;
    RETURN NEW;
END$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.