CREATE OR REPLACE FUNCTION function_create_forum()
RETURNS trigger AS
$$
BEGIN
DECLARE id_course serial;
select id INTO id_course FROM INSERTED;
insert INTO Forum (course,name,type,staff_only) VALUES
(id_course,"Staff lounge",1,true);
--forum 2 creation
--forum 3 creation
END;
$$
LANGUAGE plpgsql VOLATILE;
The trigger function should create a new forum as long a course is created, the course has a "id" field wich is serial, and that is causing problems because is not accepted inside "DECLARE" area.
Since in comments anyone continue pointing the syntax above is incorrect (regardless that is working fine in all other triggers that don't use "serial") here is another variant of the code that don't work.
CREATE OR REPLACE FUNCTION function_create_forum()
RETURNS trigger AS
$$
DECLARE
id_course int;
BEGIN
select id INTO id_course FROM INSERTED;
insert INTO Forum (course,name,type,staff_only) VALUES
(id_course,"Staff lounge",1,true);
--forum 2 creation
--forum 3 creation
END;
$$
LANGUAGE plpgsql VOLATILE;
table creation:
CREATE TABLE Forum (
course serial REFERENCES Course(id) ON DELETE CASCADE NOT NULL,
--omitted details
);
CREATE TABLE Course (
id serial UNIQUE NOT NULL, --the primary key is another column
--omitted details
)
nextval()of a sequence (but only in table context) postgresql.org/docs/current/static/… -- in other contexts, just use int.DECLARE id_course int;insertedin Postgres