2017-09-26 81 views
0

我有觸發/序列的問題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; 
+0

BTW您可以通過將列定義爲bigserial來避免觸發器+函數。 (+也許setval()) – joop

回答

0

你創建不知道它是應該找到序列功能在users_test架構中。

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' 
SET search_path = 'users_test'; 

創建函數文檔:

您可以通過指定search_path當您創建函數來進行調節。

https://www.postgresql.org/docs/9.3/static/sql-createfunction.html

0

你應該從功能使用全名(與模式): SELECT nextval('users_test.res_id_mlb_seq')

+0

謝謝我也使用它! – Reveur

相關問題