Holmar K
Holmar K

Reputation: 100

Trigger to Prevent Update on specific column

I am new at this, I kind of have no clue where I should go with this. I am trying to prevent an update on a specific column in a table. If one tries to update it aborts and throws an exception message.

    IF (TG_OP = 'UPDATE') THEN
        SELECT columnName
        FROM TableName
        ABORT;
        RAISE EXCEPTION 'Cannot modify';
END IF;

Upvotes: 1

Views: 219

Answers (1)

Lucas
Lucas

Reputation: 630

A trigger on update could simply ensure the column is the same with a comparison on the old and new version.

IF OLD.column IS DISTINCT FROM NEW.column THEN
    NEW.column := OLD.column;
END IF;

Upvotes: 1

Related Questions