[英]Obtain primary key of newly inserted row in PostgreSQL trigger
In PostgreSQL I'm trying to make a database trigger on an INSERT
that passes the primary key of the new row as the payload of a channel notification using NOTIFY
在 PostgreSQL 中,我试图在
INSERT
上创建一个数据库触发器,该触发器使用NOTIFY
将新行的主键作为通道通知的有效负载传递
I'm having trouble wrapping my head around the correct usage age of the NEW
variable in this context.在这种情况下,我很难理解
NEW
变量的正确使用年龄。 No matter what I try seems to generate a syntax error.无论我尝试什么似乎都会产生语法错误。
Below is a simplified example of my use case.下面是我的用例的简化示例。
CREATE OR REPLACE FUNCTION table_event_notify_function()
RETURNS trigger AS $$
BEGIN
NOTIFY table_event_notify, NEW.id;
return NULL;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER table_events_insert_trigger AFTER INSERT ON table_events EXECUTE PROCEDURE table_event_notify_function();
psycopg2.errors.SyntaxError: syntax error at or near "NEW"
The payload parameter for NOTIFY must be a constant. NOTIFY 的有效负载参数必须是常量。 To provide a dynamic parameter, you need to use
pg_notify()
要提供动态参数,您需要使用
pg_notify()
perform pg_notifiy('table_event_notify', new.id::txt);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.