Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-562

@OneToOne is not generating a unique constraint on the owning table

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 2.0-BETA1
    • Fix Version/s: 2.0-BETA4
    • Component/s: ORM
    • Security Level: All
    • Labels:
      None
    • Environment:
      Win 7 x64, PHP 5.3.1

      Description

      Annotations:

      <?php
      
      /** @Entity */
      class Customer
      {
          /**
           * @Id @Column(name="id", type="integer")
           */
          private $customerId;
      
          /**
           * @OneToOne(targetEntity="Cart", mappedBy="customer")
           */
          private $cart;
      }
      
      /** @Entity */
      class Cart
      {
          /**
           * @Id @Column(name="id", type="integer", nullable=false)
           */
          private $cartId;
      
          /**
           * Shouldn't unique=true be setting a UNIQUE index on customer_id ??
           *
           * @OneToOne(targetEntity="Customer", inversedBy="cart")
           * @JoinColumn(name="customer_id", referencedColumnName="id", nullable=false, unique=true)
           */
          private $customer;
      }
      

      Resulting schema (MySQL), generated with orm:schema-tool:create in CLI:

      CREATE TABLE `customer` (
        `id` int(11) NOT NULL,
        PRIMARY KEY (`id`)
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
      
      CREATE TABLE `cart` (
        `id` int(11) NOT NULL,
        `customer_id` int(11) NOT NULL,
        PRIMARY KEY (`id`),
        KEY `customer_id` (`customer_id`),  /* this should be UNIQUE KEY  */
        CONSTRAINT `cart_ibfk_1` FOREIGN KEY (`customer_id`) REFERENCES `customer` (`id`)
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8
      

      I think the key on customer_id in the cart should be generating a UNIQUE KEY by definition on a @OneToOne, with or without the unique= param in the annotation, right?

      I've tested this with and without the unique=true param and in both cases it does not generate a UNIQUE key constraint.

        Issue Links

          Activity

          Hide
          Benjamin Eberlei added a comment -

          Verified, however i cannot fix this know, we need another internal refactoring before this can be done.

          Show
          Benjamin Eberlei added a comment - Verified, however i cannot fix this know, we need another internal refactoring before this can be done.
          Hide
          Roman S. Borschel added a comment -

          Pushing back to beta4.

          Show
          Roman S. Borschel added a comment - Pushing back to beta4.
          Hide
          Roman S. Borschel added a comment -

          I think this can be easily fixed now.

          Show
          Roman S. Borschel added a comment - I think this can be easily fixed now.
          Hide
          Benjamin Eberlei added a comment -

          Fixed!

          Show
          Benjamin Eberlei added a comment - Fixed!

            People

            • Assignee:
              Benjamin Eberlei
              Reporter:
              Marc Hodgins
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: