title: How to create a Postgres trigger that calculates values
body: <p>How would you create a trigger that uses the values of the row being inserted to be calculated first so that a value being inserted gets transformed?</p>
  
  <p>Let's say I have this table <code>labor_rates</code>,</p>
  
  <pre><code>+---------------+-----------------+--------------+------------+
  | labor_rate_id | rate_per_minute | unit_minutes | created_at |
  +---------------+-----------------+--------------+------------+
  | bigint        | numeric         | numeric      | timestamp  |
  +---------------+-----------------+--------------+------------+
  </code></pre>
  
  <p>Each time a new record is created, I need that the rate is calculated as <code>rate/unit</code> (the smallest unit here is a minute).</p>
  
  <p>So example, when inserting a new record:</p>
  
  <pre><code>INSERT INTO labor_rates(rate, unit)
  VALUES (60, 480);
  </code></pre>
  
  <p>It would create a new record with these values:</p>
  
  <pre><code>+---------------+-----------------+--------------+----------------------------+
  | labor_rate_id | rate_per_minute | unit_minutes |         created_at         |
  +---------------+-----------------+--------------+----------------------------+
  |       1000000 |          1.1979 |           60 | 2017-03-16 01:59:47.208111 |
  +---------------+-----------------+--------------+----------------------------+
  </code></pre>
  
  <p>One could argue that this should be left as a calculated field instead of storing the calculated value. But in this case, it would be best if the calculated value is stored.</p>
  
  <p>I am fairly new to triggers so any help would be much appreciated.</p>