title: encrypt password trigger
body: <p>I've tried to encrypt a password in a PostgreSQL database by using a trigger but I can't get it to work, and I don't know where my mistake is.
  Here is the code:</p>
  
  <pre><code>CREATE TABLE mms_user ( 
  uid serial,
  mail text NOT NULL,
  passwd text NOT NULL,
  usertype integer NOT NULL,
  PRIMARY KEY (uid)
  );
  
  CREATE FUNCTION encrypt_password() RETURNS TRIGGER AS $$
  BEGIN
   NEW.passwd = digest(NEW.passwd, 'sha1');
   RETURN NEW; END; $$LANGUAGE 'plpgsql';
  
   CREATE TRIGGER encrypt_userdata AFTER INSERT ON mms_user EXECUTE PROCEDURE encrypt_password();
  
   INSERT INTO mms_user values (default, 'who', 'me', 1);
  </code></pre>
  
  <p>It says this when executing:</p>
  
  <blockquote>
  <pre><code>ERROR:  record "new" is not assigned yet
  DETAIL:  The tuple structure of a not-yet-assigned record is indeterminate.
  CONTEXT:  PL/pgSQL function encrypt_password() line 3 at assignment
  </code></pre>
  </blockquote>
  
  <p>How do I access the record I'm inserting if it's not with <code>NEW</code>?</p>