I want to create a trigger function in sql that would prevent update on a column in a table if the column value was not equal to "default_id". For example, if I update a row where some_id
equals to "default_id", it would succeed. However, if I update a row some_id
column that has a value that isn't equal to "default_id", it would throw an exception.
CREATE FUNCTION not_allow_update_column_some_id() RETURNS trigger
LANGUAGE plpgsql AS
$$BEGIN
IF NEW.some_id <> OLD.some_id AND OLD.some_id <> "default_id" THEN
RAISE EXCEPTION 'some_id is not allowed to be updated unless it was equal to default value';
END IF;
RETURN NEW;
END;$$;
CREATE TRIGGER not_allow_update_column_some_id
BEFORE UPDATE ON some_table FOR EACH ROW
EXECUTE PROCEDURE not_allow_update_column_some_id();
The above code doesn't seem to work as the compare seems to think "default_id" is a column instead of a value.