title: Adding a primary key to existing entity that has a unique constraint on two fields in Symfony
body: <p>I have an existing Symfony entity:</p>
  
  <pre><code>/**
   * MyApp\AppBundle\Entity\UserSelection
   *
   * @ORM\Table(name="user_selection",uniqueConstraints={@ORM\UniqueConstraint(name="unique", columns={"user_id", "selection_id"})})
   * @ORM\Entity(repositoryClass="MyApp\AppBundle\Entity\UserSelectionRepository")
   */
  class UserSelection
  {
  
      /**
       * @var integer $userId
       * @ORM\Id
       * @ORM\Column(name="user_id", type="integer")
       */
      private $userId;
  
      /**
       * @var integer $selectionId
       * @ORM\Id
       * @ORM\Column(name="selection_id", type="integer")
       */
      private $selectionId;
  }
  </code></pre>
  
  <p>I'm trying to refactor some code and when running</p>
  
  <pre><code>php app/console generate:doctrine:form MyAppAppBundle:UserSelection
  </code></pre>
  
  <p>I get </p>
  
  <blockquote>
    <p>The form generator does not support entity classes with multiple primary keys.</p>
  </blockquote>
  
  <p>...which is understandable (<a href="https://stackoverflow.com/a/22345822">thanks</a>). Is it fine to use the entity as is and manually create the form type? Or is it better to put in an id field in the entity? If I do that, what specific configuration is best?</p>
  
  <p>For example</p>
  
  <pre><code>/**
   * MyApp\AppBundle\Entity\UserSelection
   *
   * @ORM\Table(name="user_selection",uniqueConstraints={@ORM\UniqueConstraint(name="unique", columns={"user_id", "selection_id"})})
   * @ORM\Entity(repositoryClass="MyApp\AppBundle\Entity\UserSelectionRepository")
   */
  class UserSelection
  {
  
       /**
       * @var integer $id
       * @ORM\Column(name="id", type="integer")
       * @ORM\GeneratedValue(strategy="UUID")
      */
      private $id;
  
      /**
       * @var integer $userId
       * @ORM\Id
       * @ORM\Column(name="user_id", type="integer")
       */
      private $userId;
  
      /**
      * @var integer $selectionId
      * @ORM\Id
      * @ORM\Column(name="selection_id", type="integer")
      */
      private $selectionId;
  }
  </code></pre>
  
  <p>produces an error </p>
  
  <blockquote>
    <p>has a composite identifier but uses an ID generator other than manually assigning (Identity, Sequence). This is not supported.</p>
  </blockquote>
  
  <p>Then if I do</p>
  
  <pre><code>/**
   * MyApp\AppBundle\Entity\UserSelection
   *
   * @ORM\Table(name="user_selection",uniqueConstraints={@ORM\UniqueConstraint(name="unique", columns={"user_id", "selection_id"})})
   * @ORM\Entity(repositoryClass="MyApp\AppBundle\Entity\UserSelectionRepository")
   */
  class UserSelection
  {
  
       /**
       * @var integer $id
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="UUID")
      */
      private $id;
  
      /**
       * @var integer $userId
       * @ORM\Column(name="user_id", type="integer")
       */
      private $userId;
  
      /**
      * @var integer $selectionId
      * @ORM\Column(name="selection_id", type="integer")
      */
      private $selectionId;
  }
  </code></pre>
  
  <p>When I run the resulting dump-sql</p>
  
  <pre><code>ALTER TABLE user_selection ADD PRIMARY KEY (id);
  </code></pre>
  
  <p>I get </p>
  
  <blockquote>
    <p>Duplicate entry '0' for key 'PRIMARY'</p>
  </blockquote>
  
  <p>So, in terms of best practices, what is the way to proceed? </p>
  
  <p><strong>Update</strong></p>
  
  <p>So I can make this work, but it requires two steps. First, create an $id column</p>
  
  <pre><code>/**
   * @ORM\Column(type="integer")
   */
  private $id;
  </code></pre>
  
  <p>Run <code>ALTER TABLE user_selection ADD id INT NOT NULL;</code></p>
  
  <p>Then, in this order,</p>
  
  <pre><code> /**
   * @ORM\Column(type="integer")
   * @ORM\GeneratedValue(strategy="AUTO")
   * @ORM\Id
   */
  private $id;
  </code></pre>
  
  <p>Which produces <code>ALTER TABLE user_selection CHANGE id id INT AUTO_INCREMENT NOT NULL, ADD PRIMARY KEY (id);</code></p>
  
  <p>Is there a way to combine this so that all that is needed after changes to the entity is a single dump-sql command?</p>