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

前端 未结 2 1730
梦谈多话
梦谈多话 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!

提交回复
热议问题