简体   繁体   English

获取PostgreSQL触发器中新插入行的主键

[英]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.

相关问题 锁定新插入的行[PostgreSQL] - Lock the newly inserted row [PostgreSQL] PostgreSQL AFTER INSERT触发器,并引用插入的行 - PostgreSQL AFTER INSERT trigger, and referencing the inserted row postgresql - 使用刚刚创建的标识值来返回新插入的行 - postgresql - use just created identity value to return newly inserted row 如何在PostgreSQL中插入主/外键行 - How to insert a primary/foreign key row in PostgreSQL 如果主键列名称未知,如何在触发器函数中获取主键值? - How to obtain primary key value in trigger function if primary key column name is unknown? 使用QSqlTableModel :: insertRecord插入行时如何检索生成的主键 - How to retrieve generated primary key when row was inserted with QSqlTableModel::insertRecord 如何在数据库中插入新行并获得主键 - How to insert a new row into a database and obtain the primary key as a result 无法访问PostgreSQL触发器函数中的主键值 - Not able to access the Primary Key value in PostgreSQL Trigger Function 使用 C# 和 Npgsql 在 PostgreSQL 数据库上返回新插入行的 ID? - Return ID of newly inserted row on a PostgreSQL database using C# and Npgsql? 在 Postgresql 中插入行时如何自动填充外键 - How to automaticaly fill a foreign key when a row is inserted in Postgresql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM