Postgres trigger that will in any case set a value on modification?
<p>I have a Web Application that has a <code>modified</code> field in the important tables to be able to track back when any modification was done e.g. (never mind the <code>;;</code> it is there because this postgres sql code is executed from a Scala framework that uses <code>;</code> as separator and <code>;;</code> escapes it)</p>

<pre><code>CREATE TABLE security_permission (
  id BIGSERIAL,
  value VARCHAR(255) NOT NULL,
  modified TIMESTAMP DEFAULT now(),
  PRIMARY KEY (id)
);

CREATE OR REPLACE FUNCTION update_modified()
RETURNS TRIGGER AS $$
BEGIN
  NEW.modified = now();;
  RETURN NEW;;
END;;
$$ language 'plpgsql';

CREATE TRIGGER update_modified_security_permission BEFORE UPDATE ON security_permission FOR EACH ROW EXECUTE PROCEDURE update_modified();
</code></pre>

<p>The problem is this works only if the <strong>field is NOT specified</strong> in the insert/update statement. If the field is specified even with NULL then the modified is not set. I do not have full control of the generated statements because they are part of an ORM framework that generates them automatically but I'd like to nevertheless always set the modified field. How can I do that?</p>

<p>I have tried using <code>BEFORE INSERT OR UPDATE ON</code> and <code>AFTER INSERT OR UPDATE ON</code> but nothing seems to work if the field is populated in the insert/update statement even if NULL. How can I do this?</p>