PostgresSQL : Update Certain Column in Table-B After Certain Column in Table-B is Updated









up vote
0
down vote

favorite












I have 2 table in Postgres, let's say Category and Thread with relation one-to-many



Category



id | title | created_at | deleted_at 
-----+-------+------------+-----------




Thread



id | category_id | title | created_at | deleted_at 
-----+-------------+-------+------------+-----------




I want to create a trigger : if i perfom soft delete in Category (update deleted_at from null to now() ), then Thread.deleted_at should get updated too.



Here is my function



 CREATE OR REPLACE FUNCTION "thread_soft_delete"()
RETURNS trigger
AS $pg1$
BEGIN
UPDATE thread SET deleted_at = NOW()
FROM "category"
WHERE thread.category_id = "category".id;
RETURN NULL;
END;
$pg1$
VOLATILE
LANGUAGE plpgsql;


And here the trigger



 CREATE TRIGGER category_delete
AFTER UPDATE ON "category"
FOR EACH ROW
WHEN (OLD.deleted_at IS DISTINCT FROM NEW.deleted_at)
EXECUTE PROCEDURE thread_soft_delete();


When i run update "category" set deleted_at = now() WHERE id = 1;, all created_at column in thread get updated. My expectation is only rows with category_id = 1 that going to update.



Please help me










share|improve this question

















  • 2




    see: stackoverflow.com/questions/33034907/…
    – O95
    2 days ago














up vote
0
down vote

favorite












I have 2 table in Postgres, let's say Category and Thread with relation one-to-many



Category



id | title | created_at | deleted_at 
-----+-------+------------+-----------




Thread



id | category_id | title | created_at | deleted_at 
-----+-------------+-------+------------+-----------




I want to create a trigger : if i perfom soft delete in Category (update deleted_at from null to now() ), then Thread.deleted_at should get updated too.



Here is my function



 CREATE OR REPLACE FUNCTION "thread_soft_delete"()
RETURNS trigger
AS $pg1$
BEGIN
UPDATE thread SET deleted_at = NOW()
FROM "category"
WHERE thread.category_id = "category".id;
RETURN NULL;
END;
$pg1$
VOLATILE
LANGUAGE plpgsql;


And here the trigger



 CREATE TRIGGER category_delete
AFTER UPDATE ON "category"
FOR EACH ROW
WHEN (OLD.deleted_at IS DISTINCT FROM NEW.deleted_at)
EXECUTE PROCEDURE thread_soft_delete();


When i run update "category" set deleted_at = now() WHERE id = 1;, all created_at column in thread get updated. My expectation is only rows with category_id = 1 that going to update.



Please help me










share|improve this question

















  • 2




    see: stackoverflow.com/questions/33034907/…
    – O95
    2 days ago












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have 2 table in Postgres, let's say Category and Thread with relation one-to-many



Category



id | title | created_at | deleted_at 
-----+-------+------------+-----------




Thread



id | category_id | title | created_at | deleted_at 
-----+-------------+-------+------------+-----------




I want to create a trigger : if i perfom soft delete in Category (update deleted_at from null to now() ), then Thread.deleted_at should get updated too.



Here is my function



 CREATE OR REPLACE FUNCTION "thread_soft_delete"()
RETURNS trigger
AS $pg1$
BEGIN
UPDATE thread SET deleted_at = NOW()
FROM "category"
WHERE thread.category_id = "category".id;
RETURN NULL;
END;
$pg1$
VOLATILE
LANGUAGE plpgsql;


And here the trigger



 CREATE TRIGGER category_delete
AFTER UPDATE ON "category"
FOR EACH ROW
WHEN (OLD.deleted_at IS DISTINCT FROM NEW.deleted_at)
EXECUTE PROCEDURE thread_soft_delete();


When i run update "category" set deleted_at = now() WHERE id = 1;, all created_at column in thread get updated. My expectation is only rows with category_id = 1 that going to update.



Please help me










share|improve this question













I have 2 table in Postgres, let's say Category and Thread with relation one-to-many



Category



id | title | created_at | deleted_at 
-----+-------+------------+-----------




Thread



id | category_id | title | created_at | deleted_at 
-----+-------------+-------+------------+-----------




I want to create a trigger : if i perfom soft delete in Category (update deleted_at from null to now() ), then Thread.deleted_at should get updated too.



Here is my function



 CREATE OR REPLACE FUNCTION "thread_soft_delete"()
RETURNS trigger
AS $pg1$
BEGIN
UPDATE thread SET deleted_at = NOW()
FROM "category"
WHERE thread.category_id = "category".id;
RETURN NULL;
END;
$pg1$
VOLATILE
LANGUAGE plpgsql;


And here the trigger



 CREATE TRIGGER category_delete
AFTER UPDATE ON "category"
FOR EACH ROW
WHEN (OLD.deleted_at IS DISTINCT FROM NEW.deleted_at)
EXECUTE PROCEDURE thread_soft_delete();


When i run update "category" set deleted_at = now() WHERE id = 1;, all created_at column in thread get updated. My expectation is only rows with category_id = 1 that going to update.



Please help me







postgresql sql-function postgresql-triggers






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 2 days ago









Jim Johnson

162




162







  • 2




    see: stackoverflow.com/questions/33034907/…
    – O95
    2 days ago












  • 2




    see: stackoverflow.com/questions/33034907/…
    – O95
    2 days ago







2




2




see: stackoverflow.com/questions/33034907/…
– O95
2 days ago




see: stackoverflow.com/questions/33034907/…
– O95
2 days ago












1 Answer
1






active

oldest

votes

















up vote
0
down vote













You need not reference the category table in the trigger function. Just NEW.id() should be fine.



CREATE OR REPLACE FUNCTION "thread_soft_delete"()
RETURNS trigger
AS $pg1$
BEGIN
UPDATE thread SET deleted_at = NOW()
WHERE thread.category_id = NEW.id;
RETURN NULL;
END;
$pg1$
VOLATILE
LANGUAGE plpgsql;





share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53225319%2fpostgressql-update-certain-column-in-table-b-after-certain-column-in-table-b-i%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    You need not reference the category table in the trigger function. Just NEW.id() should be fine.



    CREATE OR REPLACE FUNCTION "thread_soft_delete"()
    RETURNS trigger
    AS $pg1$
    BEGIN
    UPDATE thread SET deleted_at = NOW()
    WHERE thread.category_id = NEW.id;
    RETURN NULL;
    END;
    $pg1$
    VOLATILE
    LANGUAGE plpgsql;





    share|improve this answer
























      up vote
      0
      down vote













      You need not reference the category table in the trigger function. Just NEW.id() should be fine.



      CREATE OR REPLACE FUNCTION "thread_soft_delete"()
      RETURNS trigger
      AS $pg1$
      BEGIN
      UPDATE thread SET deleted_at = NOW()
      WHERE thread.category_id = NEW.id;
      RETURN NULL;
      END;
      $pg1$
      VOLATILE
      LANGUAGE plpgsql;





      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        You need not reference the category table in the trigger function. Just NEW.id() should be fine.



        CREATE OR REPLACE FUNCTION "thread_soft_delete"()
        RETURNS trigger
        AS $pg1$
        BEGIN
        UPDATE thread SET deleted_at = NOW()
        WHERE thread.category_id = NEW.id;
        RETURN NULL;
        END;
        $pg1$
        VOLATILE
        LANGUAGE plpgsql;





        share|improve this answer












        You need not reference the category table in the trigger function. Just NEW.id() should be fine.



        CREATE OR REPLACE FUNCTION "thread_soft_delete"()
        RETURNS trigger
        AS $pg1$
        BEGIN
        UPDATE thread SET deleted_at = NOW()
        WHERE thread.category_id = NEW.id;
        RETURN NULL;
        END;
        $pg1$
        VOLATILE
        LANGUAGE plpgsql;






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 2 days ago









        krithikaGopalakrisnan

        535218




        535218



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53225319%2fpostgressql-update-certain-column-in-table-b-after-certain-column-in-table-b-i%23new-answer', 'question_page');

            );

            Post as a guest














































































            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo