三个实体和完整性约束

时间:2014-12-12 07:40:04

标签: symfony doctrine

我有三个实体:

  • 学校
  • Schooltype

以下关系:

  • 学校类型到学校(一对多)
  • 学校类型到分支(一对多)
  • 分校到学校(一对多)

如果我更新我的数据库结构" php app / console doctrine:schema:update --force"我收到以下错误消息。

  [Doctrine\DBAL\DBALException]
  An exception occurred while executing 'ALTER TABLE school ADD CONSTRAINT FK_F99EDABBDCD6CC49 FOREIGN KEY (branch_id) REFERENCES schooltype_branch (id)':
  SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`ebm2`.`#sql-3cf_e6e`, CONSTRAINT `FK_F99EDABBDCD6CC49` FOREIGN KEY (`branch_id`) REFERENCES `schooltype_branch`
   (`id`))

但我没有发现我的错误,有人可以给我一个提示吗?

我还将展示我的三个实体的一些相关部分:

学校:

class School implements BuildingInterface
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

     /**
     * @ORM\ManyToOne(targetEntity="SchoolType", inversedBy="schools")
     * @ORM\JoinColumn(name="schooltype_id", referencedColumnName="id")
     */
    protected $schooltype;

    /**
     * @ORM\ManyToOne(targetEntity="SchooltypeBranch", inversedBy="school")
     * @ORM\JoinColumn(name="branch_id", referencedColumnName="id")
     */
    protected $branch;
}

Schooltype:

class SchoolType implements BuildingTypeInterface
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\OneToMany(targetEntity="Ebm\UserBundle\Entity\School", mappedBy="schooltype")
     */
    protected $schools;

    /**
     * @ORM\OneToMany(targetEntity="Ebm\UserBundle\Entity\SchooltypeBranch", mappedBy="schooltype")
     */
    protected $branch;  
}

学校类型分支:

class SchooltypeBranch
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

     /**
     * @ORM\ManyToOne(targetEntity="SchoolType", inversedBy="branch")
     * @ORM\JoinColumn(name="schooltype_id", referencedColumnName="id")
     */
    protected $schooltype;


    /**
     * @ORM\OneToMany(targetEntity="Ebm\UserBundle\Entity\School", mappedBy="branch")
     */
    protected $school;
}

1 个答案:

答案 0 :(得分:0)

显然外键约束失败,例如school表中branch_id表中没有schooltype_branch