camrv
camrv

Reputation: 1

I want to insert a row on a table and the trigger is not working

I want to insert a row on a related table before inserting a row in the first table.

CREATE FUNCTION madre_insert_trigger() RETURNS TRIGGER AS $$
BEGIN

INSERT INTO curso_madre(nivel,curso)
VALUES('9','9'); 
EXECUTE 'INSERT INTO madre' || ' VALUES ($1.*)' USING NEW;

RETURN NULL;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER trigg_madre_insert BEFORE INSERT ON madre FOR EACH ROW EXECUTE PROCEDURE madre_insert_trigger();

table "madre":

CREATE TABLE PUBLIC.Madre(
id_Madre SERIAL,
fk_idcmadre SERIAL,
EDAD INTEGER,
ESTADO_CIVIL INTEGER,
NACIONALIDAD CHAR,
ACTIVIDAD INTEGER,
OCUPACION VARCHAR(50),
CATEGORIA INTEGER,
**CONSTRAINT pk_Madre PRIMARY KEY (id_Madre),
CONSTRAINT fk_idcmadre FOREIGN KEY (fk_idcmadre) REFERENCES PUBLIC.Curso_Madre (id_Curso)**
);

Upvotes: 0

Views: 33

Answers (1)

IVO GELOV
IVO GELOV

Reputation: 14259

Since your trigger runs BEFORE - it must return a non-NULL result, because NULL means please do not insert anything in the first table. Change it to RETURN NEW;

Upvotes: 2

Related Questions