title: How to deal with mutually dependent inserts
body: <p>I have a model that defines mutually recursive tables:</p>
  <pre><code>Answer
    questionId QuestionId
    text
  
  Question
    text
    correct AnswerId
  </code></pre>
  <p>What do I need to do to actually insert a question?  I need to know what the correct answer is first.  But to insert an answer, I need to know what question it answers.</p>
  <p>I'm running Postgres, if it matters.</p>
  <p>The DDL is:</p>
  <pre class="lang-sql prettyprint-override"><code>CREATE TABLE answer (
    id integer NOT NULL,                 -- answer id
    text character varying NOT NULL,     -- answer text
    question_id bigint NOT NULL          -- question id
  );
  
  CREATE TABLE question (
    id integer NOT NULL,                 -- question id
    question character varying NOT NULL, -- question text
    correct bigint NOT NULL,             -- correct answer
    solution character varying NOT NULL  -- solution text
  );
  
  ALTER TABLE ONLY answer ALTER COLUMN id SET DEFAULT nextval('answer_id_seq'::regclass);
  
  ALTER TABLE ONLY answer
    ADD CONSTRAINT answer_question_id_fkey FOREIGN KEY (question_id) REFERENCES question(id);
  
  ALTER TABLE ONLY question ALTER COLUMN id SET DEFAULT nextval('question_id_seq'::regclass);
  ALTER TABLE ONLY question
    ADD CONSTRAINT question_correct_fkey FOREIGN KEY (correct) REFERENCES answer(id);
  ```sql
  </code></pre>