title: Symfony Integrity constraint violation: 1062 Duplicate entry '1' for key
body: <p>I realized my error but I do not know how to solve it.
  I have two table, Car and  Advertisement, I wish performed a nested form to add data into tables. But I have an error message, which says:</p>
  
  <p>An exception occurred while executing  </p>
  
  <pre><code>INSERT INTO car (year, model, engine_size, colour, mileage, advertisement_id, fuel_id, transmission_id, body_id, make_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
  </code></pre>
  
  <p>with params <code>["2012", "a3", "v8", "45", 4, 26, 1, 1, 1, 1]</code>.</p>
  
  <p>Exception:</p>
  
  <pre><code>SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1' for key 'UNIQ_773DE69D78D28519'
  </code></pre>
  
  <p>My question is: Why my method does not retrieve the id of my entity to put it in the entity advertisement ?</p>
  
  <p><strong>Relation</strong></p>
  
  <pre><code> /**
   *
   * @ORM\OneToOne(targetEntity="Home\HomeBundle\Entity\Advertisement", cascade={"persist","remove"})
   * @ORM\JoinColumn(nullable=false)
   */
  private $Advertisement;
  
  public function setAdvertisement(\Home\HomeBundle\Entity\Advertisement $advertisement)
  {
      $this-&gt;Advertisement = $advertisement;
  
      return $this;
  }
  </code></pre>
  
  <hr>
  
  <p><strong>Form</strong></p>
  
  <pre><code>class CarType extends AbstractType
  {
  
  public function buildForm(FormBuilderInterface $builder, array $options)
  {
      $builder
          -&gt;add('year')
          -&gt;add('model')
          -&gt;add('engineSize')
          -&gt;add('colour')
          -&gt;add('mileage')
          -&gt;add('Advertisement', new AdvertisementType())
          -&gt;add('Fuel')
          -&gt;add('Transmission')
          -&gt;add('Body')
          -&gt;add('Make')
      ;
  }
  
  /**
   * @param OptionsResolverInterface $resolver
   */
  public function setDefaultOptions(OptionsResolverInterface $resolver)
  {
      $resolver-&gt;setDefaults(array(
          'data_class' =&gt; 'Home\HomeBundle\Entity\Car'
      ));
  }
  
  /**
   * @return string
   */
  public function getName()
  {
      return 'home_homebundle_car';
  }
  }
  </code></pre>
  
  <hr>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>public function createAction(Request $request)
  {
      $entity = new Car();
      $form = $this-&gt;createCreateForm($entity);
      $form-&gt;handleRequest($request);
  
      if ($form-&gt;isValid()) {
          $em = $this-&gt;getDoctrine()-&gt;getManager();
          $em-&gt;persist($entity);
          $em-&gt;flush();
  
          return $this-&gt;redirect($this-&gt;generateUrl('car_show', array('id' =&gt; $entity-&gt;getId())));
      }
  
      return $this-&gt;render('HomeBundle:Car:new.html.twig', array(
          'entity' =&gt; $entity,
          'form'   =&gt; $form-&gt;createView(),
      ));
  }
  
  /**
   * Creates a form to create a Car entity.
   *
   * @param Car $entity The entity
   *
   * @return \Symfony\Component\Form\Form The form
   */
  private function createCreateForm(Car $entity)
  {
      $form = $this-&gt;createForm(new CarType(), $entity, array(
          'action' =&gt; $this-&gt;generateUrl('car_create'),
          'method' =&gt; 'POST',
      ));
  
      $form-&gt;add('submit', 'submit', array('label' =&gt; 'Create'));
  
      return $form;
  }
  </code></pre>