0

I am a fan of adding timestamps to tables in the form of created_at, updated_at and deleted_at. But it can be tedious to add these fields to every table.

I was wondering, is it a bad design decision to create a base table every table inherits from, like this:

CREATE TABLE data(
    created_at timestamptz NOT NULL DEFAULT current_timestamp,
    updated_at timestamptz NOT NULL DEFAULT current_timestamp,
    deleted_at timestamptz
);

Or even add an ID to the base table:

CREATE TABLE data(
    id         uuid PRIMARY KEY,
    created_at timestamptz NOT NULL DEFAULT current_timestamp,
    updated_at timestamptz NOT NULL DEFAULT current_timestamp,
    deleted_at timestamptz
);

I feel like this is a hack and there must be a cleaner way to do it. If I want to automatically update updated_at in Postgres, I still have to add a trigger to every table.

Is there a possibility to add these fields to every table?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.