title: mysql circular dependency in foreign key constraints
body: <p>Given the schema:</p>
  
  <p><img src="https://i.stack.imgur.com/pnHqt.png" alt="enter image description here"></p>
  
  <p>What I need is having every <code>user_identities.belongs_to</code> reference an <code>users.id</code>.</p>
  
  <p>At the same time, every <code>users</code> has a <code>primary_identity</code> as shown in the picture.</p>
  
  <p>However when I try to add this reference with <code>ON DELETE NO ACTION ON UPDATE NO ACTION</code>, MySQL says </p>
  
  <blockquote>
    <p>#1452 - Cannot add or update a child row: a foreign key constraint fails (<code>yap</code>.<code>#sql-a3b_1bf</code>, CONSTRAINT <code>#sql-a3b_1bf_ibfk_1</code> FOREIGN KEY (<code>belongs_to</code>) REFERENCES <code>users</code> (<code>id</code>) ON DELETE NO ACTION ON UPDATE NO ACTION) </p>
  </blockquote>
  
  <p>I suspect this is due to the circular dependency, but how could I solve it (<strong>and</strong> maintain referential integrity)?</p>