title: Composite primary keys or surrogate primary key on relationship entities (entities that are also relations) in Doctrine2
body: <p>I have a large database where I have entities that are relations. This exactly because of <a href="http://docs.doctrine-project.org/en/2.0.x/reference/association-mapping.html#many-to-many-unidirectional" rel="nofollow noreferrer">the following comment</a> in the Doctrine 2 manual: I needed to store additional attributes in our relations and because of this the relation became an entity.</p>
  
  <p><strong><em>Normally we use generated ids for entities and composite keys for relationships. In this case the entity is a relationship, so that makes the choice a bit more complicated...</em></strong></p>
  
  <p>Be aware of the fact that I'm not talking about natural keys here. I am using a composite key which consists out of id's which are integers created using an auto increment strategy in their respective owning entities.</p>
  
  <p>I see some <strong>disadvantages</strong> of using <strong>composite primary keys</strong> on the relationship entity:</p>
  
  <ul>
  <li>I need to call persist in between creating the entities and their relations (to generate the key on the entity before I can use it as a foreign key in my composite primary key).</li>
  <li>To relate additional entities to the relationship entity we need to add columns for the ids used in the composite key again in those entities. If I would use a surrogate primary key I only need one column which holds that one id.</li>
  </ul>
  
  <p>I see also some <strong>advantages</strong> of the using <strong>composite keys</strong> for example:</p>
  
  <ul>
  <li>They eliminate the need for certain joins queries, instead you can build queries to directly extract information using the composite keys.</li>
  </ul>
  
  <p>But the <strong>most important argument</strong> for using composite keys for now is the following: </p>
  
  <ul>
  <li>When a relationship between a User and SomeEntity gets deleted and later created again (a scenario which is likely to happen in our application model) we need to restore access to all other related entities too.
  If the relationship between User and SomeEntity is created using an auto increment strategy on a surrogate primary key. The restored (new) relationship will get a new primary key (id). Because of this the related entities (stored using the "old" key) are no longer accessible.
  If I'd use a composite key on the relationship I could simply restore the relationship (it gets the same composite key values) and by that I have immediately access to the other related entities again. </li>
  </ul>
  
  <p>Are there any people who have experience with a similar scenario? Are there other solutions to get this working properly. Are there any important disadvantages or advantages (when using Doctrine ORM) that I missed.</p>
  
  <p>One alternative I can think of is by adding a state to the the User SomeEntity relationship. Instead of deleting the entry we could change the state to disabled, enabled. In that way we could use a surrogate key instead of a composite key because the entry will never be deleted...</p>
  
  <p>Feedback is greatly appreciated...</p>
  
  <p>For the people that are interested there was also a nice discussion on surrogate vs composite keys on Stack Overflow here: <a href="https://stackoverflow.com/questions/159087/composite-primary-keys-versus-unique-object-id-field">(click)</a></p>