Thread: Trigger won't accept function (Please Help)

Trigger won't accept function (Please Help)

From
Micah Woods
Date:
I've created a plpgsql function that takes 2 args as follows:

CREATE FUNCTION insert_default(text,text) RETURNS OPAQUE AS 'BEGIN  new.$2 =
new.$1;  RETURN new;  END;' LANGUAGE 'plpgsql';

When I try to reference this function when creating a trigger:

CREATE TRIGGER "iep_district_insert" BEFORE INSERT ON "iep_district" FOR
EACH ROW EXECUTE PROCEDURE insert_default('id_author','id_author_last_mod');

I get this error: PostgreSQL said: ERROR: CreateTrigger: function
insert_default() does not exist

Questions:

1. I can ONLY reference functions in triggers that have NO args, any
function (like above example) that takes 1 or more args always gets the does
not exist error.

2. I'm new to postgres, perhaps there's a better way have the default value
of a field be that of another field??

Thanks for any help,

Micah
--
Micah Woods
Woods/IT LLC
2214 Waite Ave.
Kalamazoo, MI 49008

v: 616 349-1175
f: 877 349-4929 (toll free)
e: micah@woods-it.com
w: http://www.woods-it.com



Re: Trigger won't accept function (Please Help)

From
Jörg Wallerich
Date:
Hi,

triggers call their registered functions always using the parameter-less
version. For Postgres, func() and func(text) are two entirely different
things! Also, trigger functions get an implicit set of parameters, like
NEW etc. (see documentation).

IMHO, this should be done as follows:

CREATE FUNCTION insert_default() RETURNS OPAQUE AS '
BEGIN
    NEW.id_author_last_mod = NEW.id_author;
    return NEW;
END;'
LANGUAGE 'plpgsql';

CREATE TRIGGER "iep_district_insert" BEFORE INSERT ON "iep_district"
FOR EACH ROW EXECUTE PROCEDURE insert_default();


 J.W.


Micah Woods wrote:
>
> I've created a plpgsql function that takes 2 args as follows:
>
> CREATE FUNCTION insert_default(text,text) RETURNS OPAQUE AS 'BEGIN
> new.$1;  RETURN new;  END;' LANGUAGE 'plpgsql';
>
> When I try to reference this function when creating a trigger:
>
> CREATE TRIGGER "iep_district_insert" BEFORE INSERT ON "iep_district" FOR
> EACH ROW EXECUTE PROCEDURE insert_default('id_author','id_author_last_mod');
>
> I get this error: PostgreSQL said: ERROR: CreateTrigger: function
> insert_default() does not exist
>
> Questions:
>
> 1. I can ONLY reference functions in triggers that have NO args, any
> function (like above example) that takes 1 or more args always gets the does
> not exist error.
>
> 2. I'm new to postgres, perhaps there's a better way have the default value
> of a field be that of another field??
>
> Thanks for any help,
>
> Micah
> --
> Micah Woods
> Woods/IT LLC
> 2214 Waite Ave.
> Kalamazoo, MI 49008
>
> v: 616 349-1175
> f: 877 349-4929 (toll free)
> e: micah@woods-it.com
> w: http://www.woods-it.com
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: Have you searched our list archives?
>
> http://www.postgresql.org/search.mpl