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
postgresql sql-function postgresql-triggers
add a comment |
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
postgresql sql-function postgresql-triggers
2
see: stackoverflow.com/questions/33034907/…
– O95
2 days ago
add a comment |
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
postgresql sql-function postgresql-triggers
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
postgresql sql-function postgresql-triggers
asked 2 days ago
Jim Johnson
162
162
2
see: stackoverflow.com/questions/33034907/…
– O95
2 days ago
add a comment |
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
add a comment |
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;
add a comment |
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;
add a comment |
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;
add a comment |
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;
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;
answered 2 days ago
krithikaGopalakrisnan
535218
535218
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
2
see: stackoverflow.com/questions/33034907/…
– O95
2 days ago