寻找有关PostgreSQL触发器的一些建议和帮助。我有3个表,我正在尝试使用触发器。
以下是他们的描述:
\d staging_assets;
Table "public.staging_assets"
Column | Type | Modifiers
---------------------+-----------------------+---------------
pipe_id | character varying(25) | not null
kind | character varying(25) |
pipe_jt_num | character varying(25) |
pipe_heat | character varying(25) |
pipe_length | numeric(4,2) |
pipe_wall_thickness | numeric(4,3) | default 0.000
degree | numeric(4,2) | default 0
Indexes:
"staging_assets_pkey" PRIMARY KEY, btree (pipe_id)
Check constraints:
"kind_chk" CHECK (upper(kind::text) = 'BEND'::text OR upper(kind::text) = 'PIPE_JOINT'::text) NO INHERIT
Triggers:
distinquish AFTER INSERT ON staging_assets FOR EACH ROW EXECUTE PROCEDURE move_along()
\d bend_assets;
Table "public.bend_assets"
Column | Type | Modifiers
----------------+-----------------------+---------------
custom_id | character varying(20) | not null
bnd_pipe_id | character varying(20) |
bnd_heat | character varying(20) |
bnd_length | numeric(3,1) |
degree | numeric(3,1) |
wall_thickness | numeric(4,3) | default 0.000
Indexes:
"bend_assets_pkey" PRIMARY KEY, btree (custom_id)
Check constraints:
"bnd_len_chk" CHECK (bnd_length > 0.0)
Referenced by:
TABLE "weld_log" CONSTRAINT "weld_log_bnd_c
onnect_fkey" FOREIGN KEY (bnd_connect) REFERENCES bend_assets(custom_id)
\d pup_assets;
Table "public.pup_assets"
Column | Type | Modifiers
--------------------+-----------------------+---------------
custom_pup_id | character varying(20) | not null
pup_pipe_id | character varying(20) |
pup_heat | character varying(20) |
pup_length | numeric(3,1) |
pup_wall_thickness | numeric(4,3) | default 0.000
Indexes:
"pup_assets_pkey" PRIMARY KEY, btree (custom_pup_id)
Check constraints:
"pup_len_chk" CHECK (pup_length > 0.0)
Referenced by:
TABLE "weld_log" CONSTRAINT "weld_log_pup2_connect_fkey" FOREIGN KEY (pup2_connect) REFERENCES pup_assets(custom_pup_id)
TABLE "weld_log" CONSTRAINT "weld_log_pup_connect_fkey" FOREIGN KEY (pup1_connect) REFERENCES pup_assets(custom_pup_id)
我的目标是无论何时将记录插入staging_assets
表,它都会插入pup_assets
或bend_assets
表中,具体取决于staging_assets
中的值。 1}} kind
列。我对条件逻辑使用CASE
语句。
这是我到目前为止尝试的触发器功能:
CREATE OR REPLACE FUNCTION move_along()
RETURNS trigger
AS $CORRECTING$
BEGIN
CASE
WHEN NEW.kind = 'BEND'
THEN INSERT INTO bend_assets(custom_id, bnd_pipe_id, bnd_heat, bnd_length, degree, wall_thickness)
SELECT NEW.pipe_id, NEW.pipe_jt_num, NEW.pipe_heat, NEW.pipe_length, NEW.degree, NEW.pipe_wall_thickness
FROM staging_assets sa;
RETURN NEW;
WHEN NEW.kind = 'PIPE_JOINT'
THEN INSERT INTO pup_assets(custom_pup_id, pup_pipe_id, pup_heat, pup_length, pup_wall_thickness)
SELECT NEW.pipe_id, NEW.pipe_jt_num, NEW.pipe_heat, NEW.pipe_length, NEW.pipe_wall_thickness
FROM staging_assets sa;
RETURN NEW;
ELSE
RAISE NOTICE 'Not a valid kind type';
END CASE;
RETURN NULL;
END;
$CORRECTING$
LANGUAGE PLpgSQL;
实际的触发器定义是:
CREATE TRIGGER distinquish
AFTER INSERT
ON staging_assets
FOR EACH row
EXECUTE PROCEDURE move_along();
为PIPE_JOINT'尝试单个插入时kind
这有效,并且会将记录插入pup_assets
表:
INSERT INTO staging_assets
VALUES('Joint-1','PIPE_JOINT','9892','Heat-61',18.45,0.290,0);
INSERT 0 1
fab_tracking=> TABLE staging_assets;
pipe_id | kind | pipe_jt_num | pipe_heat | pipe_length | pipe_wall_thickness | degree
---------+------------+-------------+-----------+-------------+---------------------+--------
Joint-1 | PIPE_JOINT | 9892 | Heat-61 | 18.45 | 0.290 | 0.00
(1 row)
fab_tracking=> TABLE pup_assets;
custom_pup_id | pup_pipe_id | pup_heat | pup_length | pup_wall_thickness
---------------+-------------+----------+------------+--------------------
Joint-1 | 9892 | Heat-61 | 18.5 | 0.290
(1 row)
再次为“BEND”执行单个插入操作。 kind
值有效:
INSERT INTO staging_assets
VALUES('Joint-2','BEND','1892','Heat-41',10.45,0.290,7.50);
INSERT 0 1
fab_tracking=> TABLE staging_assets;
pipe_id | kind | pipe_jt_num | pipe_heat | pipe_length | pipe_wall_thickness | degree
---------+------------+-------------+-----------+-------------+---------------------+--------
Joint-1 | PIPE_JOINT | 9892 | Heat-61 | 18.45 | 0.290 | 0.00
Joint-2 | BEND | 1892 | Heat-41 | 10.45 | 0.290 | 7.50
(2 rows)
fab_tracking=> TABLE bend_assets;
custom_id | bnd_pipe_id | bnd_heat | bnd_length | degree | wall_thickness
-----------+-------------+----------+------------+--------+----------------
Joint-2 | 1892 | Heat-41 | 10.5 | 7.5 | 0.290
(1 row)
然而,当我尝试多次插入时,我遇到了问题:
INSERT INTO staging_assets
VALUES ('Joint-3', 'BEND', '8989A', 'Heat-171', 12.33, 0.387, 5.75),
('Joint-4', 'PIPE_JOINT', '4984A', 'Heat-141', 22.33, 0.387, 0.0);
ERROR: duplicate key value violates unique constraint "pup_assets_pkey"
DETAIL: Key (custom_pup_id)=(Joint-4) already exists.
CONTEXT: SQL statement "INSERT INTO pup_assets(custom_pup_id, pup_pipe_id, pup_heat, pup_length, pup_wall_thickness)
SELECT NEW.pipe_id, NEW.pipe_jt_num, NEW.pipe_heat, NEW.pipe_length, NEW.pipe_wall_thickness
FROM staging_assets sa"
PL/pgSQL function move_along() line 8 at SQL statement
我很困惑为什么我在pup_assets
表中获取Joint-4的唯一约束的错误键以及为什么触发器不能使用多个插入。
检查pup_assets
表时,那里没有Joint-4。
fab_tracking=> TABLE pup_assets;
custom_pup_id | pup_pipe_id | pup_heat | pup_length | pup_wall_thickness
---------------+-------------+----------+------------+--------------------
Joint-1 | 9892 | Heat-61 | 18.5 | 0.290
(1 row)
寻找任何帮助,以了解为什么失败以及如何修复它以执行多个插入。我很感激任何建议。谢谢。