B
B
Bogdan2017-08-03 17:47:48
PostgreSQL
Bogdan, 2017-08-03 17:47:48

Automatic completion of fields?

Hello. How to correctly implement that when a record is inserted into the table, the created_at, updated_at fields are automatically updated. And when updating records, only updated_at. Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Burov, 2017-08-03
@bogdan_uman

https://www.postgresql.org/docs/current/static/ddl...
When updating either by a trigger or in the update query itself.

0
0xD34F, 2017-08-03
@0xD34F

Hang a trigger , inside there will be something like:BEFORE INSERT OR UPDATE

NEW.updated_at = now();
IF TG_OP = 'INSERT' THEN
  NEW.created_at = now();
END IF;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question