我有觸發/序列的問題PostgreSQL的PostgreSQL的錯誤:關係不存在
當我嘗試INSERT在我的表res_letterbox我有這樣的錯誤:
ERROR: relation "res_id_mlb_seq" does not exist
LINE 1: SELECT nextval('res_id_mlb_seq') QUERY: SELECT nextval('res_id_mlb_seq') CONTEXT: PL/pgSQL function users_test.trigger_fct_t_res_letterbox_ins() line 5 at SQL statement********** Error **********
ERROR: relation "res_id_mlb_seq" does not exist SQL state: 42P01
Context: PL/pgSQL function users_test.trigger_fct_t_res_letterbox_ins() line 5 at SQL statement
我試圖設置直接在列RES_ID的defaut值: NEXTVAL( 'res_id_mlb_seq' :: regclass的),它返回我:
ERROR:relation "res_id_mlb_seq" does not exist
-
CREATE SCHEMA users_test;
ALTER SCHEMA users_test OWNER TO users_test;
SET search_path = users_test;
CREATE TABLE res_letterbox (
res_id bigint,
title varchar(255) DEFAULT 'NULL',
...
..
);
CREATE SEQUENCE res_id_mlb_seq INCREMENT 1 MINVALUE 1 NO MAXVALUE START 12602;
DROP TRIGGER IF EXISTS t_res_letterbox_ins ON res_letterbox CASCADE;
CREATE OR REPLACE FUNCTION trigger_fct_t_res_letterbox_ins() RETURNS trigger AS $BODY$
BEGIN
BEGIN
SELECT nextval('res_id_mlb_seq')
INTO NEW.res_id;
EXCEPTION
WHEN OTHERS
THEN
RAISE;
END;
RETURN NEW;
END
$BODY$
LANGUAGE 'plpgsql';
CREATE TRIGGER t_res_letterbox_ins
BEFORE INSERT ON res_letterbox FOR EACH ROW
EXECUTE PROCEDURE trigger_fct_t_res_letterbox_ins();
ALTER SEQUENCE IF EXISTS res_id_mlb_seq RESTART WITH 12603;
BTW您可以通過將列定義爲bigserial來避免觸發器+函數。 (+也許setval()) – joop