【问题标题】:Three entities and integrity constraint三实体和完整性约束
【发布时间】:2014-12-12 07:40:03
【问题描述】:

我有三个实体:

  • 学校
  • 学校类型
  • 分公司

具有以下关系:

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

如果我更新我的数据库结构“php app/console dictionary: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;
}

学校类型:

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;
}

【问题讨论】:

    标签: symfony doctrine


    【解决方案1】:

    显然,外键约束失败,例如 school 表中有 branch_id 不存在于 schooltype_branch 表中。

    【讨论】:

    • 但我定义了学校和分支之间的引用,例如 @ORM\JoinColumn(name="branch_id", referencedColumnName="id")。 "branch_id" 只存在于与 SchooltypeBranch Entity 的 id 相关的 School Entity 中。
    • 您的架构是否有效? php 应用程序\控制台原则:模式:验证
    • 现在我解决了这个问题。我从表“Schooltype_branch”中删除所有记录并重建表结构。现在一切都很好,非常感谢您的支持
    猜你喜欢
    • 1970-01-01
    • 2012-07-11
    • 1970-01-01
    • 1970-01-01
    • 2015-04-11
    • 2020-01-14
    • 2021-08-03
    • 1970-01-01
    相关资源
    最近更新 更多