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

前端 未结 2 1722
梦谈多话
梦谈多话 2021-02-12 09:47

I have a trigger function:

CREATE OR REPLACE FUNCTION \"trigger_deleteUsers\"()
RETURNS trigger AS
$BODY$
BEGIN
    INSERT INTO \"DeletedEntities\" (\"uuidKey\",         


        
相关标签:
2条回答
  • 2021-02-12 10:05

    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!

    0 讨论(0)
  • 2021-02-12 10:08

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

    0 讨论(0)
提交回复
热议问题