未在OneToOne上创建唯一约束

时间:2015-03-17 11:38:48

标签: symfony doctrine-orm

在Doctrine中使用一对一关系时,我认为学说通常会在列上创建一个唯一的约束? (这样可以防止多条记录具有相同的外来ID,从而防止这种关系成为多对一)

我很确定我过去曾亲眼目睹过这种行为。目前关系表现得像多对一,这不是我想要的

以下是我的实体概述(我也使用表继承,因此也包含了这些实体)

<?php

/**
 * @ORM\Entity()
 */
class IplanLayerImage extends ChildIplanLayerImage
{
    /**
     * @ORM\OneToOne(targetEntity="IplanLayer", inversedBy="iplanLayerImage")
     * @ORM\JoinColumn(name="image_iplan_layer_id", referencedColumnName="id", nullable=true)
     **/
    protected $imageIplanLayer;

    /* .................. */
}


/**
 * @ORM\Table(name="iplan_layer")
 * @ORM\Entity()
 */
class IplanLayer
{
    /**
     * @ORM\OneToOne(targetEntity="IplanLayerImage", mappedBy="imageIplanLayer")
     **/
    protected $iplanLayerImage;

    /* .................. */

}

/**
 * @ORM\Entity
 */
class ChildIplanLayerImage extends Upload
{
    /* .................. */
}


/**
* @ORM\Entity
* @ORM\Table("upload")
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="discr", type="string")
* @ORM\Entity()
* @ORM\DiscriminatorMap({
*          "photo" = "Photo",
*          "child_photo" = "ChildPhoto",
*          "iplan_layer_image" = "IplanLayerImage",
*          "child_iplan_layer_image" = "ChildIplanLayerImage"
*          })
*/
abstract class Upload
{
    /* ........ */
}

我希望在“upload”表(IplanLayerImage实体)中,列image_iplan_layer_id对它有唯一约束。但是,“app / console doctrine:schema:update”生成的SQL看起来更像这样(更新是针对空数据库运行的)

CREATE TABLE upload (
  id                     INT AUTO_INCREMENT NOT NULL,
  image_iplan_layer_id   INT          DEFAULT NULL,
  ...........
  INDEX IDX_17BDE61F8FD952F1 (image_iplan_layer_id),
  PRIMARY KEY (id)
)

CREATE TABLE iplan_layer (
  id         INT AUTO_INCREMENT NOT NULL,
  ...........
  INDEX IDX_14E915C1BE04EA9 (job_id),
  PRIMARY KEY (id)
)

ALTER TABLE upload ADD CONSTRAINT FK_17BDE61F8FD952F1 FOREIGN KEY (image_iplan_layer_id) REFERENCES iplan_layer (id);

为什么没有创建唯一约束的任何提示?

Symfony 2.6.3

Doctrine 2.5

0 个答案:

没有答案