How can I get the table name in a PostgreSQL trigger function?

守給你的承諾、 提交于 2019-12-03 23:29:56

TG_TABLE_NAME. See the docs for other trigger arguments: http://www.postgresql.org/docs/current/static/plpgsql-trigger.html

try this:

TG_TABLE_NAME::regclass::text

I use it with version 9.4 but it should be working from 8.4 up.

Here is your code with this change:

CREATE OR REPLACE FUNCTION "trigger_deleteUsers"()
RETURNS trigger AS
$BODY$
BEGIN
  INSERT INTO "DeletedEntities" ("uuidKey", "dateCreated", "dateModified", "dateSynced", "username", "entityName")
  VALUES (OLD."uuidKey", OLD."dateCreated", OLD."dateModified", "dateSynced", OLD."username", TG_TABLE_NAME::regclass::text);

  RETURN NULL;
END;
$BODY$
LANGUAGE plpgsql;

CREATE TRIGGER "deleteUsers" AFTER DELETE ON "Users" FOR EACH ROW EXECUTE 
PROCEDURE "trigger_deleteUsers"();

let me know if it helps!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!