M
M
Melnik2282021-04-03 11:09:12
PostgreSQL
Melnik228, 2021-04-03 11:09:12

How to return one deleted row to the table in a trigger?

let's say you can delete any rows from the table, except for some specific one with the value of the key field = 1,

if you do a BEFORE trigger FOR EACH ROW, then you just check the value of the key field against the OLD table, and if = 1, then just do nothing, so ?

and if by means of AFTER?
then how is it, if the row has already been deleted (you can also check through OLD or through the table itself) - then you can only roll back the entire transaction? that is, return other rows that have a field != 1?
Here it is impossible to return only that at which = 1?
or it is possible, but only through an insertion (INSERT through OLD) of this line back to the table simply?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
galaxy, 2021-04-03
@Melnik228

Again engaged in the removal of tonsils through the ass?

if you do a BEFORE trigger FOR EACH ROW, then it's just to check the value of the key field against the OLD table, and if = 1, then just do nothing, right?

Yes, but not "do nothing", but return NULL from the trigger function. This is the correct way.
and if by means of AFTER?

AFTER triggers are not intended for modifying or canceling operations, they are usually used for auditing, summing up pivot tables, and so on.
The trigger can cancel the completed AFTER operation only by throwing an exception (yes, the entire INSERT/UPDATE/DELETE query will be rolled back).
or it is possible, but only through an insertion (INSERT through OLD) of this line back to the table simply?

Unfortunately, you can. Postgres allows you to pick a table from a trigger on it. But it's a great way to shoot yourself in the foot. For example, this is how you can call an infinite recursive trigger call. You can get unexpected results: for example, I'm not sure what kind of table state the FOR EACH ROW trigger sees (there are doubts about FOR EACH STATEMENT) - at the beginning of the transaction? operations? Or already after processing some lines? all lines?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question