title: Prevent insert if condition is met
body: <p>I have a table <code>Content</code> like this:</p>
  
  <pre><code>id | text | date | idUser → User | contentType 
  </code></pre>
  
  <p>And another table <code>Answer</code>:</p>
  
  <pre><code>idAnswer → Content | idQuestion → Content | isAccepted
  </code></pre>
  
  <p>I want to ensure that the <code>Answer</code>'s date is bigger than the <code>Question</code>'s date. A question is a <code>Content</code> with <code>contentType</code> = 'QUESTION'.</p>
  
  <p>I tried to solve this with the following trigger, but when I try to insert an <code>Answer</code> there's an error: </p>
  
  <blockquote>
  <pre><code>ERROR:  record "new" has no field "idanswer"
  CONTEXT:  SQL statement "SELECT (SELECT "Content".date FROM "Content" WHERE "Content".id = NEW.idAnswer) &lt; (SELECT "Content".date FROM "Content" WHERE "Content".id = NEW.idQuestion)"
  PL/pgSQL function "check_valid_date_answer" line 2 at IF
  </code></pre>
  </blockquote>
  
  <p>Trigger:</p>
  
  <pre><code>CREATE TRIGGER check_valid_answer 
  AFTER INSERT ON "Answer"
  FOR EACH ROW EXECUTE PROCEDURE check_valid_date_answer();
  </code></pre>
  
  <p>Trigger function:</p>
  
  <pre><code>CREATE FUNCTION check_valid_date_answer() RETURNS trigger
      LANGUAGE plpgsql
      AS $$BEGIN
    IF (SELECT "Content".date FROM "Content"
        WHERE "Content".id = NEW.idAnswer)
     &lt; (SELECT "Content".date FROM "Content"
        WHERE "Content".id = NEW.idQuestion) 
    THEN
      RAISE NOTICE 'This Answer is an invalid date';
    END IF;
    RETURN NEW;
  END;$$;
  </code></pre>
  
  <p>So, my question is: do I really need to create a trigger for this? I saw that I can't use a <code>CHECK</code> in <code>Answer</code> because I need to compare with an attribute of another table. Is there any other (easier/better) way to do this? If not, why the error and how can I solve it?</p>