title: Primary key of owning side as a join column
body: <p>NOTE:  <em>Topic is lengthy but detailed and may come in handy if you use Doctrine2 and oneToOne relationships.</em></p>
  
  <p>Recently I came across a problem in Doctrine:</p>
  
  <p>I created User and UserData objects with oneToOne bidirectional relationship:</p>
  
  <pre><code>User:
  ...
    oneToOne:
      userdata:
        targetEntity: UserData
        mappedBy: user
  
  UserData:
  ...
    oneToOne:
      user:
        targetEntity: User
        inversedBy: userdata
  </code></pre>
  
  <p>So UserData was the owning side with user_id column in it:</p>
  
  <pre><code>user: id, ...
  userdata: id, user_id, ...
  </code></pre>
  
  <p>This created a problem, where every time you fetch a User object (Single user, collection of user or collection of other object with user joined on it) Doctrine would lazy load a UserObject for each User. </p>
  
  <p>Issue described here: </p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/9735930/how-to-prevent-doctrine-from-lazy-loading-one-to-one-relationsip">How to prevent Doctrine from lazy loading one to one relationsip?</a></li>
  <li><a href="http://groups.google.com/group/doctrine-user/browse_thread/thread/7e421a2b189f0ea7" rel="nofollow noreferrer">http://groups.google.com/group/doctrine-user/browse_thread/thread/7e421a2b189f0ea7</a></li>
  <li><a href="https://github.com/doctrine/doctrine2/issues/4389" rel="nofollow noreferrer">https://github.com/doctrine/doctrine2/issues/4389</a></li>
  </ul>
  
  <p>Proposed solution described here:</p>
  
  <ul>
  <li><a href="https://github.com/doctrine/doctrine2/issues/2364" rel="nofollow noreferrer">https://github.com/doctrine/doctrine2/issues/2364</a></li>
  </ul>
  
  <p>So there are 3 ways around this:</p>
  
  <ol>
  <li>Wait and see if proposed solution is addressed in Doctrine and fixed in future releases (may not happen)</li>
  <li>Manually left join UserData to User in every query (still waste of resources, dont need UserData)</li>
  <li>Switch inverse side and make User the owning side.</li>
  </ol>
  
  <p>I decided to go with #3. So my schema relationship now looks like this:</p>
  
  <pre><code>User:
  ...
    oneToOne:
      userdata:
        targetEntity: UserData
        inversedBy: user
  
  UserData:
  ...
    oneToOne:
      user:
        targetEntity: User
        mappedBy: userdata
  </code></pre>
  
  <p>This means that my tables now look like this:</p>
  
  <pre><code>user: id, userdata_id, ...
  userdata: id, ...
  </code></pre>
  
  <p>I decided that instead of having Userdata.id autoincremented, I'll set it manually and match it with user.id. This means that UserData.id will always match user.id. </p>
  
  <p><strong>Question</strong> Can I use user.id (a primary autoincremented key) as joinColum instead of userdata_id since they will always have the same value? Do you see any potential issues with this way of doing things?</p>
  
  <p>Any other tips or opinions about this issue is greatly welcomed and appreciated.</p>